Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 438 Bytes

Valid_Perfect_Square.md

File metadata and controls

18 lines (16 loc) · 438 Bytes

367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Code:

class Solution {
    public boolean isPerfectSquare(int num) {
        long r = num;
        while (r * r > num) {
            r = (r + num / r) / 2;
        }
        return r * r == num;
    }
}

解题思路

  • 使用Newton's Method解决平方根问题。