A B-S c++ Solution


  • 0
    P
    class Solution {
    public:
        int mySqrt(int x) {
            if(x==1)
                return 1;
            double tmp = x/2;
            double q = tmp;
            while(abs(tmp*tmp-x) > 0.2 ){
                if(tmp*tmp > x)
                    tmp -= q/2;
                if(tmp*tmp <x)
                    tmp += q/2;
                q /=2;
            }
            return int(tmp);
        }
    };

Log in to reply
 

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