Beats 90%


  • 0
    P
    public class Solution {
        public int mySqrt(int x) {
           if(x == 0)
            return x;
           double dividend = x;
           double val = x;
           double last;
           do
           {
               last = val;
               val=(val + dividend/val)*0.5;
           }while(Math.abs(val - last) > 1e-9);
           return (int)val;
        }
    }

Log in to reply
 

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