-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfSquareNumbers.java
More file actions
67 lines (56 loc) · 1.55 KB
/
SumOfSquareNumbers.java
File metadata and controls
67 lines (56 loc) · 1.55 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package leetcode.medium;
/**
* https://leetcode.com/problems/sum-of-square-numbers/description/?envType=daily-question&envId=2024-06-17
* <p>
* Example 1:
* <p>
* Input: c = 5 Output: true Explanation: 1 * 1 + 2 * 2 = 5 Example 2:
* <p>
* Input: c = 3 Output: false
*/
public class SumOfSquareNumbers {
public boolean judgeSquareSum(int c) {
for (int a = 0; a * a <= c; a++) {
int b = c - a * a;
if (isPerfectSquare(b)) {
return true;
}
}
return false;
}
private static boolean isPerfectSquare(int b) {
long sqrt = (long) Math.sqrt(b);
return sqrt * sqrt == b;
}
public boolean judgeSquareSumOptimized(int c) {
long left = 0;
long right = (long) Math.sqrt(c);
while (left <= right) {
long sum = left * left + right * right;
if (sum == c) {
return true;
} else if (sum < c) {
left++;
} else {
right--;
}
}
return false;
}
public static void main(String[] args) {
SumOfSquareNumbers solution = new SumOfSquareNumbers();
// Test cases
int num1 = 5;
int num2 = 13;
int num3 = 3;
System.out.println(
num1 + " can be expressed as the sum of two squares: " + solution.judgeSquareSumOptimized(
num1));
System.out.println(
num2 + " can be expressed as the sum of two squares: " + solution.judgeSquareSumOptimized(
num2));
System.out.println(
num3 + " can be expressed as the sum of two squares: " + solution.judgeSquareSumOptimized(
num3));
}
}