Posted on

Description

Submission

typedef long long ll;

class Solution {
public:
    bool isPerfectSquare(int num) {
        if(num == 1) return true;

        for(ll left = 1, right = num; left <= right; ) {
            if(left == right) {
                if(left * left == num) return true;
                return false;
            }
            ll mid = (left + right) / 2;
            ll s = mid * mid;
            if(s == num) return true;
            if(s < num) {
                left = mid + 1;
            } 

            if(s > num) {
                right = mid - 1;
            }


        }
        return false;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *