1: class Solution { 2: public: 3: bool isPerfectSquare(int num) { 4: int lo = 1, hi = num; 5: while (lo <= hi) { 6:
long long mid = lo + (hi - lo) / 2;
7: if (mid*mid == num) return true; 8: else if (mid*mid > num) hi = mid-1; 9: else lo = mid+1; 10: } 11: return false; 12: } 13: };
No comments:
Post a Comment