A neat java binary search sol


  • 0
    H
    public int mySqrt(int x) {
            if(x<=1) return x;
            int l=1, r=x-1;
            while(l<r){
                int m=l+((r-l+1)>>1);
                if(m>x/m) r=m-1;
                else l=m;
            }
            return l;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.