【代码随想录刷题记录】LeetCode367有效的完全平方数-2. 代码

时间:2024-04-24 22:21:48
class Solution {
public:
    //左闭右闭
    bool isPerfectSquare(int num) {
        int left = 0;
        int right = num;
        while(left <= right)
        {
            int middle = left + ((right - left) >> 1);
            if(num > long(middle) * long(middle))
            {
                left = middle + 1;
            }
            else if(num == long(middle) * long(middle))
            {
                return true;
            }
            else
            {
                right = middle - 1;
            }
        }
        return false;
    }
};

结果也是顺利通过