Leetcode-633 (两数平方和)

Posted mustardseed

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-633 (两数平方和)相关的知识,希望对你有一定的参考价值。

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 (两数平方和)的主要内容,如果未能解决你的问题,请参考以下文章

双指针 - 两数之和 & 两数平方和

leetcode 633. 平方数之和双指针,指针遍历方向相反

leetcode 633. 平方数之和双指针,指针遍历方向相反

leetcode 633. 平方数之和双指针,指针遍历方向相反

leetcode 633. 平方数之和双指针,指针遍历方向相反

leetcode 做过的题目总结