367. Valid Perfect Square

来源:互联网 发布:淘宝哪个牌子芋圆好吃 编辑:程序博客网 时间:2024/05/26 17:48
class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        i = 1
        while num > 0:
            num -= i
            i += 2

        return num == 0

http://www.cnblogs.com/guoguolan/p/5619443.html


完全平方数是一系列奇数之和,例如:

1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
25 = 1 + 3 + 5 + 7 + 9
36 = 1 + 3 + 5 + 7 + 9 + 11
....
1+3+...+(2n-1) = (2n-1 + 1)n/2 = n*n


原创粉丝点击