LeetCode: Sum of Square Numbers

LeetCode: 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:

1
2
3
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

1
2
Input: 3
Output: False
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Solution {
public boolean judgeSquareSum(int c) {
if(c==0) return true;
for (int i = 0; i < Math.sqrt(c); i++) {
int x = c - i * i;
int a = (int) Math.sqrt(x);
if (a * a == x) {
return true;
}
}
return false;
}
}