-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumOfSquareNumbers.java
More file actions
34 lines (28 loc) · 953 Bytes
/
SumOfSquareNumbers.java
File metadata and controls
34 lines (28 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Given a non-negative integer c, your task is to decide whether there're
// two integers a and b such that a2 + b2 = c.
// See: https://leetcode.com/problems/sum-of-square-numbers/
package leetcode.math;
public class SumOfSquareNumbers {
public boolean judgeSquareSum(int c) {
for (int i = 0; i <= Math.sqrt(c); i++) {
double sqrt = Math.sqrt(c - i * i);
if (sqrt == (int)sqrt)
return true;
}
return false;
}
public boolean judgeSquareSum_var1(int c) {
for (int i = 0; i <= Math.sqrt(c); i++) {
for (int j = 0; j <= Math.sqrt(c); j++) {
if (i * i + j*j == c) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
SumOfSquareNumbers sln = new SumOfSquareNumbers();
System.out.println(sln.judgeSquareSum(15));
}
}