Leetcode69x的平方根

1106

什么时候要加long啊..

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

你可能感兴趣的:(算法,java,leetcode)