标签:tco input note while any https ali span ret
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt
.
Example 1:
Input: 16 Returns: True
Example 2:
Input: 14 Returns: False
Use Newton Method to calculate the square root or num, refer to Newton Method for details.
Code
class Solution: def validSquare(self, n): ans = n while ans * ans > n: ans = (ans + n/ans)//2 return ans **2 == n
[LeetCode] 367. Valid Perfect Square_Easy tag:Math
标签:tco input note while any https ali span ret
原文地址:https://www.cnblogs.com/Johnsonxiong/p/9510836.html