Valid Perfect Square

2019-05-25 22:59:26 浏览数 (1)

1. Description

2. Solution

代码语言:javascript复制
class Solution {
public:
    bool isPerfectSquare(int num) {
        int left = 0;
        int right = num;
        while(left <= right) {
            long mid = (left   right) / 2;
            long square = mid * mid;
            if(square == num) {
                return true;
            }
            if(square > num) {
                right = mid - 1;
            }
            else {
                left = mid   1;
            }
        }
        return false;
    }
};

0 人点赞