-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower-of-three.cpp
54 lines (47 loc) · 1.09 KB
/
power-of-three.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
class Solution {
public:
bool isPowerOfThree(int n)
{
if (0 >= n)
return false;
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
bool isPowerOfThree4(int n) {
if(n<=0) return false;
while(n!=1){
if(n%3) return false;
n/=3;
}
return true;
}
// https://www.acwing.com/activity/content/problem/content/194/1/
bool isPowerOfThree3(int n) {
return n > 0 && 1162261467 % n == 0;
}
bool isPowerOfThree2(int n) {
if(n==0) return false;
if(n==1) return true;
double num=n;
while(num>1){
num/=3.0;
}
return num==1;
}
bool isPowerOfThree1(int n) {
if(n==0) return false;
if(n==1) return true;
priority_queue<long long> p;
p.emplace(3);
while(!p.empty()){
auto t=p.top();
if(t==n) return true;
if(t>n) return false;
p.pop();
p.emplace(3*t);
}
return false;
}
};