Skip to content

Commit 1452fb5

Browse files
committed
869
1 parent a8c5f70 commit 1452fb5

File tree

3 files changed

+6
-77
lines changed

3 files changed

+6
-77
lines changed

leetcode/869. Reordered Power of 2/README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ Traverse all the permutations of N, and examine each of them whether it is power
4040
```cpp
4141
// OJ: https://leetcode.com/problems/reordered-power-of-2/
4242
// Author: github.com/lzl124631x
43-
// Time: O(lg(N)! * log(N)),
44-
// where lg(N) is number of digits in N base-10,
45-
// and log(N) is number of digits in N base-2.
46-
// O(lg(N)!) is because of permutation, log(N) is because of testing `isPow2`.
43+
// Time: O(lg(N)!)
4744
// Space: O(lg(N))
4845
class Solution {
4946
private:
5047
bool isPow2(long long N) {
51-
while (N != 1) {
52-
if (N % 2) return false;
53-
N /= 2;
54-
}
55-
return true;
48+
return (N & (N - 1)) == 0;
5649
}
5750
string num;
5851
bool dfs(int start) {
@@ -80,7 +73,7 @@ public:
8073
8174
## Solution 2. Counting
8275
83-
There are only 32 possible powers of 2. Count the digits of N and compare the counts with those of a power of 2.
76+
There are only 32 possible powers of 2. Count the digits of `N` and compare the counts with those of a power of 2.
8477
8578
```cpp
8679
// OJ: https://leetcode.com/problems/reordered-power-of-2/
@@ -91,8 +84,8 @@ There are only 32 possible powers of 2. Count the digits of N and compare the co
9184
// Space: O(lg(N))
9285
class Solution {
9386
private:
94-
vector<int> count(int N) {
95-
vector<int> cnt(10, 0);
87+
array<int, 10> count(int N) {
88+
array<int, 10> cnt = {};
9689
while (N) {
9790
cnt[N % 10]++;
9891
N /= 10;
@@ -104,7 +97,7 @@ public:
10497
auto cnt = count(N);
10598
for (int i = 0; i < 31; ++i) {
10699
auto c = count(1 << i);
107-
if (equal(cnt.begin(), cnt.end(), c.begin())) return true;
100+
if (cnt == c) return true;
108101
}
109102
return false;
110103
}

leetcode/869. Reordered Power of 2/s1.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

leetcode/869. Reordered Power of 2/s2.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)