JAVA--------------Easy Version To UnderStand!!!!!!!


  • 0
    H
        public int mySqrt(int x) {
        if (x < 0)
    		return -1;
    	if (x <= 1)
    		return x;
    	int low = 1, high = x;
    	while (low <=high) {
    		int mid = low + (high - low) / 2;
    		if (mid == x / mid)
    			return mid;
    		else if (mid < x / mid)
    			low = mid + 1;
    		else
    			high = mid - 1;
    	}
    	return high;
    }

Log in to reply
 

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