Skip to content

Commit 937a86f

Browse files
committed
feat: add solutions to lc problem: No.0869
No.0869.Reordered Power of 2
1 parent 28073f8 commit 937a86f

File tree

8 files changed

+269
-89
lines changed

8 files changed

+269
-89
lines changed

solution/0800-0899/0869.Reordered Power of 2/README.md

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一
60+
### 方法一:枚举
61+
62+
我们可以在 $[1, 10^9]$ 的范围内枚举所有的 $2$ 的幂,判断它们的数字组成是否与给定的数字相同。
63+
64+
定义一个函数 $f(x)$,表示数字 $x$ 的数字组成。我们可以将数字 $x$ 转换为一个长度为 $10$ 的数组,或者一个按数字大小排序的字符串。
65+
66+
首先,我们计算给定数字 $n$ 的数字组成 $\text{target} = f(n)$。然后,我们枚举 $i$ 从 1 开始,每次将 $i$ 左移一位(相当于乘以 $2$),直到 $i$ 超过 $10^9$。对于每个 $i$,我们计算它的数字组成,并与 $\text{target}$ 进行比较。如果相同,则返回 $\text{true}$;如果枚举结束仍未找到相同的数字组成,则返回 $\text{false}$。
67+
68+
时间复杂度 $O(\log^2 M)$,空间复杂度 $O(\log M)$。其中 $M$ 是本题的输入范围上限 ${10}^9$。
6169

6270
<!-- tabs:start -->
6371

@@ -66,16 +74,17 @@ tags:
6674
```python
6775
class Solution:
6876
def reorderedPowerOf2(self, n: int) -> bool:
69-
def convert(n):
77+
def f(x: int) -> List[int]:
7078
cnt = [0] * 10
71-
while n:
72-
n, v = divmod(n, 10)
79+
while x:
80+
x, v = divmod(x, 10)
7381
cnt[v] += 1
7482
return cnt
7583

76-
i, s = 1, convert(n)
84+
target = f(n)
85+
i = 1
7786
while i <= 10**9:
78-
if convert(i) == s:
87+
if f(i) == target:
7988
return True
8089
i <<= 1
8190
return False
@@ -86,19 +95,19 @@ class Solution:
8695
```java
8796
class Solution {
8897
public boolean reorderedPowerOf2(int n) {
89-
String s = convert(n);
90-
for (int i = 1; i <= Math.pow(10, 9); i <<= 1) {
91-
if (s.equals(convert(i))) {
98+
String target = f(n);
99+
for (int i = 1; i <= 1000000000; i <<= 1) {
100+
if (target.equals(f(i))) {
92101
return true;
93102
}
94103
}
95104
return false;
96105
}
97106

98-
private String convert(int n) {
107+
private String f(int x) {
99108
char[] cnt = new char[10];
100-
for (; n > 0; n /= 10) {
101-
cnt[n % 10]++;
109+
for (; x > 0; x /= 10) {
110+
cnt[x % 10]++;
102111
}
103112
return new String(cnt);
104113
}
@@ -111,17 +120,23 @@ class Solution {
111120
class Solution {
112121
public:
113122
bool reorderedPowerOf2(int n) {
114-
vector<int> s = convert(n);
115-
for (int i = 1; i <= pow(10, 9); i <<= 1)
116-
if (s == convert(i))
123+
string target = f(n);
124+
for (int i = 1; i <= 1000000000; i <<= 1) {
125+
if (target == f(i)) {
117126
return true;
127+
}
128+
}
118129
return false;
119130
}
120131

121-
vector<int> convert(int n) {
122-
vector<int> cnt(10);
123-
for (; n; n /= 10) ++cnt[n % 10];
124-
return cnt;
132+
private:
133+
string f(int x) {
134+
char cnt[10] = {};
135+
while (x > 0) {
136+
cnt[x % 10]++;
137+
x /= 10;
138+
}
139+
return string(cnt, cnt + 10);
125140
}
126141
};
127142
```
@@ -130,21 +145,72 @@ public:
130145
131146
```go
132147
func reorderedPowerOf2(n int) bool {
133-
convert := func(n int) []byte {
134-
cnt := make([]byte, 10)
135-
for ; n > 0; n /= 10 {
136-
cnt[n%10]++
137-
}
138-
return cnt
139-
}
140-
s := convert(n)
141-
for i := 1; i <= 1e9; i <<= 1 {
142-
if bytes.Equal(s, convert(i)) {
148+
target := f(n)
149+
for i := 1; i <= 1000000000; i <<= 1 {
150+
if bytes.Equal(target, f(i)) {
143151
return true
144152
}
145153
}
146154
return false
147155
}
156+
157+
func f(x int) []byte {
158+
cnt := make([]byte, 10)
159+
for x > 0 {
160+
cnt[x%10]++
161+
x /= 10
162+
}
163+
return cnt
164+
}
165+
```
166+
167+
#### TypeScript
168+
169+
```ts
170+
function reorderedPowerOf2(n: number): boolean {
171+
const f = (x: number) => {
172+
const cnt = Array(10).fill(0);
173+
while (x > 0) {
174+
cnt[x % 10]++;
175+
x = (x / 10) | 0;
176+
}
177+
return cnt.join(',');
178+
};
179+
const target = f(n);
180+
for (let i = 1; i <= 1_000_000_000; i <<= 1) {
181+
if (target === f(i)) {
182+
return true;
183+
}
184+
}
185+
return false;
186+
}
187+
```
188+
189+
#### Rust
190+
191+
```rust
192+
impl Solution {
193+
pub fn reordered_power_of2(n: i32) -> bool {
194+
fn f(mut x: i32) -> [u8; 10] {
195+
let mut cnt = [0u8; 10];
196+
while x > 0 {
197+
cnt[(x % 10) as usize] += 1;
198+
x /= 10;
199+
}
200+
cnt
201+
}
202+
203+
let target = f(n);
204+
let mut i = 1i32;
205+
while i <= 1_000_000_000 {
206+
if target == f(i) {
207+
return true;
208+
}
209+
i <<= 1;
210+
}
211+
false
212+
}
213+
}
148214
```
149215

150216
<!-- tabs:end -->

solution/0800-0899/0869.Reordered Power of 2/README_EN.md

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Enumeration
56+
57+
We can enumerate all powers of 2 in the range $[1, 10^9]$ and check if their digit composition is the same as the given number.
58+
59+
Define a function $f(x)$ that represents the digit composition of number $x$. We can convert the number $x$ into an array of length 10, or a string sorted by digit size.
60+
61+
First, we calculate the digit composition of the given number $n$ as $\text{target} = f(n)$. Then, we enumerate $i$ starting from 1, shifting $i$ left by one bit each time (equivalent to multiplying by 2), until $i$ exceeds $10^9$. For each $i$, we calculate its digit composition and compare it with $\text{target}$. If they are the same, we return $\text{true}$; if the enumeration ends without finding the same digit composition, we return $\text{false}$.
62+
63+
Time complexity $O(\log^2 M)$, space complexity $O(\log M)$. Where $M$ is the upper limit of the input range ${10}^9$ for this problem.
5664

5765
<!-- tabs:start -->
5866

@@ -61,16 +69,17 @@ tags:
6169
```python
6270
class Solution:
6371
def reorderedPowerOf2(self, n: int) -> bool:
64-
def convert(n):
72+
def f(x: int) -> List[int]:
6573
cnt = [0] * 10
66-
while n:
67-
n, v = divmod(n, 10)
74+
while x:
75+
x, v = divmod(x, 10)
6876
cnt[v] += 1
6977
return cnt
7078

71-
i, s = 1, convert(n)
79+
target = f(n)
80+
i = 1
7281
while i <= 10**9:
73-
if convert(i) == s:
82+
if f(i) == target:
7483
return True
7584
i <<= 1
7685
return False
@@ -81,19 +90,19 @@ class Solution:
8190
```java
8291
class Solution {
8392
public boolean reorderedPowerOf2(int n) {
84-
String s = convert(n);
85-
for (int i = 1; i <= Math.pow(10, 9); i <<= 1) {
86-
if (s.equals(convert(i))) {
93+
String target = f(n);
94+
for (int i = 1; i <= 1000000000; i <<= 1) {
95+
if (target.equals(f(i))) {
8796
return true;
8897
}
8998
}
9099
return false;
91100
}
92101

93-
private String convert(int n) {
102+
private String f(int x) {
94103
char[] cnt = new char[10];
95-
for (; n > 0; n /= 10) {
96-
cnt[n % 10]++;
104+
for (; x > 0; x /= 10) {
105+
cnt[x % 10]++;
97106
}
98107
return new String(cnt);
99108
}
@@ -106,17 +115,23 @@ class Solution {
106115
class Solution {
107116
public:
108117
bool reorderedPowerOf2(int n) {
109-
vector<int> s = convert(n);
110-
for (int i = 1; i <= pow(10, 9); i <<= 1)
111-
if (s == convert(i))
118+
string target = f(n);
119+
for (int i = 1; i <= 1000000000; i <<= 1) {
120+
if (target == f(i)) {
112121
return true;
122+
}
123+
}
113124
return false;
114125
}
115126

116-
vector<int> convert(int n) {
117-
vector<int> cnt(10);
118-
for (; n; n /= 10) ++cnt[n % 10];
119-
return cnt;
127+
private:
128+
string f(int x) {
129+
char cnt[10] = {};
130+
while (x > 0) {
131+
cnt[x % 10]++;
132+
x /= 10;
133+
}
134+
return string(cnt, cnt + 10);
120135
}
121136
};
122137
```
@@ -125,21 +140,72 @@ public:
125140
126141
```go
127142
func reorderedPowerOf2(n int) bool {
128-
convert := func(n int) []byte {
129-
cnt := make([]byte, 10)
130-
for ; n > 0; n /= 10 {
131-
cnt[n%10]++
132-
}
133-
return cnt
134-
}
135-
s := convert(n)
136-
for i := 1; i <= 1e9; i <<= 1 {
137-
if bytes.Equal(s, convert(i)) {
143+
target := f(n)
144+
for i := 1; i <= 1000000000; i <<= 1 {
145+
if bytes.Equal(target, f(i)) {
138146
return true
139147
}
140148
}
141149
return false
142150
}
151+
152+
func f(x int) []byte {
153+
cnt := make([]byte, 10)
154+
for x > 0 {
155+
cnt[x%10]++
156+
x /= 10
157+
}
158+
return cnt
159+
}
160+
```
161+
162+
#### TypeScript
163+
164+
```ts
165+
function reorderedPowerOf2(n: number): boolean {
166+
const f = (x: number) => {
167+
const cnt = Array(10).fill(0);
168+
while (x > 0) {
169+
cnt[x % 10]++;
170+
x = (x / 10) | 0;
171+
}
172+
return cnt.join(',');
173+
};
174+
const target = f(n);
175+
for (let i = 1; i <= 1_000_000_000; i <<= 1) {
176+
if (target === f(i)) {
177+
return true;
178+
}
179+
}
180+
return false;
181+
}
182+
```
183+
184+
#### Rust
185+
186+
```rust
187+
impl Solution {
188+
pub fn reordered_power_of2(n: i32) -> bool {
189+
fn f(mut x: i32) -> [u8; 10] {
190+
let mut cnt = [0u8; 10];
191+
while x > 0 {
192+
cnt[(x % 10) as usize] += 1;
193+
x /= 10;
194+
}
195+
cnt
196+
}
197+
198+
let target = f(n);
199+
let mut i = 1i32;
200+
while i <= 1_000_000_000 {
201+
if target == f(i) {
202+
return true;
203+
}
204+
i <<= 1;
205+
}
206+
false
207+
}
208+
}
143209
```
144210

145211
<!-- tabs:end -->
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
class Solution {
22
public:
33
bool reorderedPowerOf2(int n) {
4-
vector<int> s = convert(n);
5-
for (int i = 1; i <= pow(10, 9); i <<= 1)
6-
if (s == convert(i))
4+
string target = f(n);
5+
for (int i = 1; i <= 1000000000; i <<= 1) {
6+
if (target == f(i)) {
77
return true;
8+
}
9+
}
810
return false;
911
}
1012

11-
vector<int> convert(int n) {
12-
vector<int> cnt(10);
13-
for (; n; n /= 10) ++cnt[n % 10];
14-
return cnt;
13+
private:
14+
string f(int x) {
15+
char cnt[10] = {};
16+
while (x > 0) {
17+
cnt[x % 10]++;
18+
x /= 10;
19+
}
20+
return string(cnt, cnt + 10);
1521
}
16-
};
22+
};

0 commit comments

Comments
 (0)