-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path279.java
More file actions
45 lines (44 loc) · 1.23 KB
/
279.java
File metadata and controls
45 lines (44 loc) · 1.23 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int numSquares(int n) {
if(is_sqrt(n))
return 1;
while(n%4==0) {
n/=4;
}
if(n%8==7)
return 4;
for(int i=0;i*i<n;i++) {
if(is_sqrt(n-i*i))
return 2;
}
return 3;
}
public static boolean is_sqrt(int n) {
int m = (int)Math.sqrt(n);
if (m*m == n)
return true;
else
return false;
}
}
__________________________________________________________________________________________________
sample 32024 kb submission
class Solution {
public int numSquares(int n) {
while ( n%4 == 0 )
n/=4;
if ( ((int)Math.sqrt(n))*((int)Math.sqrt(n)) == n )
return 1;
if ( n%8 == 7 )
return 4;
for ( int i = 1; i*i < n; i++ ){
int x = n-i*i;
if ( ((int)Math.sqrt(x))*((int)Math.sqrt(x)) == x )
return 2;
}
return 3;
}
}
__________________________________________________________________________________________________