【LeetCode刷题-二分查找】--69.x的平方根

69.x的平方根

【LeetCode刷题-二分查找】--69.x的平方根_第1张图片

class Solution {
    public int mySqrt(int x) {
        int l = 0,r = x ,ans = -1;
        while(l <=  r){
            int mid = (r - l)/2 + l;
            if((long)mid * mid <=x)
            {
                ans = mid;
                l = mid + 1;
            }else{
                r = mid - 1;
            }
        }
        return ans;
    }
}

你可能感兴趣的:(#,二分查找,leetcode,算法)