Here is my c++ solution by binary search


  • 0
    I
    class Solution {
    public:
        int mySqrt(int x) {
            
            if(x == 1)
                return 1;
            int start = 1, end = x/2; 
            while(start <= end){
                int mid = (start+end)/2;
                if(mid == x/mid)
                    return mid;
                else if(mid < x/mid)
                    start = mid+1;
                else
                    end = mid-1;
            }
            
            return end;
            //0 1 2 3 4 5   3
        }
    };
    

Log in to reply
 

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