633. Sum of Square Numbers 一个数是否能由两个数相加所得

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3Output: False
  1. class Solution(object):
  2. def judgeSquareSum(self, c):
  3. """
  4. :type c: int
  5. :rtype: bool
  6. """
  7. maxs = 1
  8. while(maxs * maxs) < c:
  9. maxs += 1
  10. low = 0
  11. high = maxs
  12. while low <= high:
  13. if ((low * low) + (high * high)) == c:
  14. return True
  15. if ((low * low) + (high * high)) < c:
  16. low += 1
  17. if ((low * low) + (high * high)) > c:
  18. high -= 1
  19. return False




原文地址:https://www.cnblogs.com/xiejunzhao/p/7173128.html