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: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3 Output: False
题目含义:给一个数字c,判断是否满足a平方+b平方=c
思路:a和b最大为c开根号,最小为0.
1 public boolean judgeSquareSum(int c) { 2 int a=0; 3 int b=(int)Math.sqrt(c); 4 while (a<=b) 5 { 6 double sum = a*a + b*b; 7 if (sum ==c) return true; 8 if (sum<c) a++; 9 else b--; 10 } 11 return false; 12 }