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

Leetcode-633 (两数平方和)

时间:2020-04-09 10:35:12      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:art   star   情况   lse   ==   end   循环   div   square   

1.题目描述:

判断一个非负整数是否为两个整数的平方和。

 

2.同Leetcode167,使用双指针来解题

import math
class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        if c < 0:
            return False

        #如果c不是0
        start = 0
        end = int(math.sqrt(c))+1
        while start <= end:
            if start*start + end *end ==c:
                return True
            elif start*start + end *end >c:
                end-=1
            else:
                start+=1
        else:
            return False

 

3.需要注意的点:

1.start 的值可以从0开始

2.number可以为0,且容易遗漏number为负数的情况

3.math.sqrt(c)为非整数,要转换成整数int(math.sqrt(c))

4.while循环条件中,start和end可以相等这种情况,也容易遗漏

5.题目看似简单,容易做错,细节很容易遗漏

Leetcode-633 (两数平方和)

标签:art   star   情况   lse   ==   end   循环   div   square   

原文地址:https://www.cnblogs.com/Mustardseed/p/12664979.html

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