zoukankan      html  css  js  c++  java
  • 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.题目看似简单,容易做错,细节很容易遗漏

  • 相关阅读:
    按回车键提交表单
    Access数据库类型及属性
    Problem 1002
    问题 1003
    Problem 1003
    Switch Game(摘自LP学C++)
    1006
    膜拜蛇形矩阵
    A == B?
    Rectangles
  • 原文地址:https://www.cnblogs.com/Mustardseed/p/12664979.html
Copyright © 2011-2022 走看看