Skip to content

Commit dbd7d34

Browse files
committed
Solution for sum of squeare numbers
1 parent bb3b717 commit dbd7d34

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/leetcode/SumofSquareNumbers.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode;
2+
3+
public class SumofSquareNumbers {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
public boolean judgeSquareSum(int c) {
11+
int limit = helper(c);
12+
int r =limit;
13+
for(int i=0; i<=limit && r>=i;){
14+
int sum =i*i + r*r;
15+
if(sum==c){
16+
return true;
17+
}else if(sum<c){
18+
i++;
19+
}else{
20+
r--;
21+
}
22+
}
23+
24+
return false;
25+
}
26+
27+
public int helper(int x){
28+
if(x<=1) return x;
29+
int start=0, end=x;
30+
while(start<=end) {
31+
int mid = start + (end-start)/2;
32+
if(x/mid==mid)
33+
return mid;
34+
else if(x/mid<mid)
35+
end = mid-1;
36+
else
37+
start = mid+1;
38+
}
39+
return end;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)