-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_4 Power of four.cpp
78 lines (51 loc) · 1.33 KB
/
Day_4 Power of four.cpp
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
68
69
70
71
72
73
74
75
76
77
78
//Power of 4
//Solution 1 Time O(log4(num)) Space O(1)
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<1) return false;
while(num>1)
{
if(num%4 !=0) return false;
num=num/4;
}
return true;
}
};
//Solution 1 Time O(log2(num)) Space O(1)
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<1)
return false;
bitset <32> n=num;
if(n.count() !=1) return false;
int cnt=0;
while(num)
{
num=num>>1;
cnt++;
}
if(cnt%2==0) return false;
return true;
}
};
//Solution 3 Time O(1) Space O(1)
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<1)
return false;
bitset <32> n=num;
if(n.count() !=1) return false;
if((num+1)%3==0) return false;
return true;
}
};
//Solution 4 Time O(1) Space O(1)
class Solution {
public:
bool isPowerOfFour(int num) {
return num>0 && (num&(num-1))==0 && (num & 0b01010101010101010101010101010101);
}
};