Skip to content

Latest commit

 

History

History
17 lines (17 loc) · 516 Bytes

69.md

File metadata and controls

17 lines (17 loc) · 516 Bytes

以为跟367一样的,然鹅要把0和1的特殊情况加上 二分的循环条件也要改一改~控制left为结果的右面一个数

class Solution {
public:
    int mySqrt(int num) {
        int left = 0, right = min(num, 46340);   //sqrt(2147483647),  avoid overflow
        while(left<=right){
            int mid = (left+right) >> 1;
            if(mid*mid > num) right = mid-1;
            else if(mid*mid < num) left = mid+1;
            else return mid;
        }
        return left-1;
    }
};