-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path367.java
More file actions
50 lines (48 loc) · 1.37 KB
/
367.java
File metadata and controls
50 lines (48 loc) · 1.37 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
__________________________________________________________________________________________________
sample 0 ms submission
/*
* @lc app=leetcode id=367 lang=java
*
* [367] Valid Perfect Square
*/
class Solution {
public boolean isPerfectSquare(int num) {
if (num == 1) {
return true;
}
long start = 1, end = num;
while (start < end - 1) {
long middle = start + (end - start) / 2;
long temp = middle * middle;
if (temp == (long)num) {
return true;
} else if (temp > (long)num) {
end = middle;
} else {
start = middle;
}
}
return start * start == num || end * end == num;
}
}
__________________________________________________________________________________________________
sample 31424 kb submission
class Solution {
public boolean isPerfectSquare(int num) {
int low=1, high=num, mid;
while(low<=high){
mid=low+(high-low)/2;
if(num/mid < mid){
high=mid-1;
}
else{
if(mid*mid==num){
return true;
}
low=mid+1;
}
}
return false;
}
}
__________________________________________________________________________________________________