Java Solution


  • 0
    R

    class Solution {
    public int mySqrt(int x) {

        double upperBound = x;
        double lowerBound = 1;
        
        while((upperBound - lowerBound) > 0.00001) {
             upperBound = (upperBound + lowerBound) /2;
            
             lowerBound = x / upperBound;
        }
        
        return (int)upperBound;
    }
    

    }


  • 0
    G

    public int mySqrt(int x) {
    if(x < 4) return x == 0 ? 0 : 1;
    int res = 2 * mySqrt(x/4);
    if((res+1) * (res+1) <= x && (res+1) * (res+1) >= 0) return res+1;
    return res;
    }


Log in to reply
 

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