Java Solution


  • 0
    L
        public int mySqrt(int x) {
            if(x==1) {
                return 1;
            }
            
            int e = x/2;
            int s = 0;
            int p =0 ;
            
            while(e>=s) {
                p = (e-s)/2 + s;
                if((long)p*p == (long)x) {
                    return p;
                }
                else if((long)p*p < (long)x) {
                    s = p+1;
                }
                else {
                    e = p-1;
                }
            }
            
            return e;//return e because it is the closest full square within the range.
        }
    

Log in to reply
 

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