forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (58 loc) · 1.55 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// Source : https://leetcode.com/problems/reordered-power-of-2/description/
/// Author : liuyubobobo
/// Time : 2018-07-14
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/// Permutation all possibility and check
/// Using next_permutation:)
///
/// Time Complexity: O((logN)! * logN)
/// Space Complexity: O(logN)
class Solution {
public:
bool reorderedPowerOf2(int N) {
vector<int> digits = getDigits(N);
sort(digits.begin(), digits.end());
do{
if(digits[0] == 0)
continue;
if(isPower2(digits))
return true;
}while(next_permutation(digits.begin(), digits.end()));
return false;
}
private:
bool isPower2(const vector<int>& digits){
int num = 0;
for(int digit: digits)
num = num * 10 + digit;
return isPower2(num);
}
bool isPower2(int x){
for(int i = 0 ; i <= 30 ; i ++)
if(x == (1<<i))
return true;
return false;
}
vector<int> getDigits(int N){
vector<int> res;
while(N){
res.push_back(N % 10);
N /= 10;
}
return res;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
print_bool(Solution().reorderedPowerOf2(1));
print_bool(Solution().reorderedPowerOf2(10));
print_bool(Solution().reorderedPowerOf2(16));
print_bool(Solution().reorderedPowerOf2(24));
print_bool(Solution().reorderedPowerOf2(46));
return 0;
}