码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 367. Valid Perfect Square_Easy tag:Math

时间:2018-08-21 15:11:23      阅读:132      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!