-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPowerOfFour.java
42 lines (38 loc) · 892 Bytes
/
PowerOfFour.java
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
/*https://leetcode.com/problems/power-of-four/*/
//binary search
class Solution {
public boolean isPowerOfFour(int n) {
if (n < 0) return false;
int low = 0, high = 15, mid, pow;
while (low <= high)
{
mid = low+(high-low)/2;
pow = (int)Math.pow(4,mid);
if (pow == n) return true;
else if (pow > n) high = mid-1;
else low = mid+1;
}
return false;
}
}
//recursion
class Solution {
public boolean isPowerOfFour(int n) {
if(n==0){
return false;
}
if (n==1){
return true ;
}
if (n%4!=0){
return false;
}
return isPowerOfFour(n/4);
}
}
//one liner
class Solution {
public boolean isPowerOfFour(int n) {
return (n > 0) && ((n&(n-1)) == 0) && ((n-1)%3 == 0);
}
}