-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
52 lines (46 loc) · 1.01 KB
/
main.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
class Solution {
public:
bool isPowerOfFour(int n) {
if (n <= 0) return false;
while (n > 1) {
if (n % 4) return false;
n /= 4;
}
return true;
}
};
class Solution {
public:
bool isPowerOfFour(int n) {
long long v = 1;
while (v < n) v *= 4;
return v == n;
}
};
class Solution {
public:
bool isPowerOfFour(int n) {
if (n <= 0) return false;
double v = log(n) / log(4);
return v == (int)v;
}
};
class Solution {
public:
bool isPowerOfFour(int n) {
if (n <= 0) return false;
if (__builtin_popcount(n) != 1) return false;
if (__builtin_ctz(n) % 2) return false;
return true;
}
};
class Solution {
unordered_set<int> powerOfFour;
public:
Solution() {
for (long long x = 1; x <= numeric_limits<int>::max(); x <<= 2) {
powerOfFour.insert(x);
}
}
bool isPowerOfFour(int n) { return powerOfFour.count(n) > 0; }
};