Java Accepted Solution using Newton's method


  • 0
        public boolean isPerfectSquare(int num) {
            double x = num;
            
            while (true) {
                double last = x;
                x = x - (x*x-num)/(2*x);
                if (Math.abs(x-last) < 0.25) break;
            }
            
            int res = (int)x;
            if (res*res == num) return true;
            return false;
        }

Log in to reply
 

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