Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 20a8d76

Browse files
committed
feat: add solutions to lc problem: No.0869
No.0869.Reordered Power of 2
1 parent 12d41e7 commit 20a8d76

File tree

6 files changed

+234
-4
lines changed

6 files changed

+234
-4
lines changed

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

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
<li><code>1 &lt;= N &lt;= 10^9</code></li>
5454
</ol>
5555

56-
5756
## 解法
5857

5958
<!-- 这里可写通用的实现逻辑 -->
@@ -65,15 +64,95 @@
6564
<!-- 这里可写当前语言的特殊实现逻辑 -->
6665

6766
```python
68-
67+
class Solution:
68+
def reorderedPowerOf2(self, n: int) -> bool:
69+
def convert(n):
70+
counter = [0] * 10
71+
while n > 0:
72+
counter[n % 10] += 1
73+
n //= 10
74+
return counter
75+
76+
i, s = 1, convert(n)
77+
while i <= 10 ** 9:
78+
if convert(i) == s:
79+
return True
80+
i <<= 1
81+
return False
6982
```
7083

7184
### **Java**
7285

7386
<!-- 这里可写当前语言的特殊实现逻辑 -->
7487

7588
```java
89+
class Solution {
90+
public boolean reorderedPowerOf2(int n) {
91+
String s = convert(n);
92+
for (int i = 1; i <= Math.pow(10, 9); i <<= 1) {
93+
if (s.equals(convert(i))) {
94+
return true;
95+
}
96+
}
97+
return false;
98+
}
99+
100+
private String convert(int n) {
101+
char[] counter = new char[10];
102+
while (n > 0) {
103+
++counter[n % 10];
104+
n /= 10;
105+
}
106+
return new String(counter);
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
bool reorderedPowerOf2(int n) {
117+
vector<int> s = convert(n);
118+
for (int i = 1; i <= pow(10, 9); i <<= 1)
119+
if (s == convert(i)) return true;
120+
return false;
121+
}
122+
123+
vector<int> convert(int n) {
124+
vector<int> counter(10);
125+
while (n)
126+
{
127+
++counter[n % 10];
128+
n /= 10;
129+
}
130+
return counter;
131+
}
132+
};
133+
```
76134

135+
### **Go**
136+
137+
```go
138+
func reorderedPowerOf2(n int) bool {
139+
convert := func(n int) []byte {
140+
counter := make([]byte, 10)
141+
for n > 0 {
142+
counter[n%10]++
143+
n /= 10
144+
}
145+
return counter
146+
}
147+
148+
s := convert(n)
149+
for i := 1; i <= 1e9; i <<= 1 {
150+
if bytes.Equal(s, convert(i)) {
151+
return true
152+
}
153+
}
154+
return false
155+
}
77156
```
78157

79158
### **...**

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

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,100 @@
5151
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
5252
</ul>
5353

54-
5554
## Solutions
5655

5756
<!-- tabs:start -->
5857

5958
### **Python3**
6059

6160
```python
62-
61+
class Solution:
62+
def reorderedPowerOf2(self, n: int) -> bool:
63+
def convert(n):
64+
counter = [0] * 10
65+
while n > 0:
66+
counter[n % 10] += 1
67+
n //= 10
68+
return counter
69+
70+
i, s = 1, convert(n)
71+
while i <= 10 ** 9:
72+
if convert(i) == s:
73+
return True
74+
i <<= 1
75+
return False
6376
```
6477

6578
### **Java**
6679

6780
```java
81+
class Solution {
82+
public boolean reorderedPowerOf2(int n) {
83+
String s = convert(n);
84+
for (int i = 1; i <= Math.pow(10, 9); i <<= 1) {
85+
if (s.equals(convert(i))) {
86+
return true;
87+
}
88+
}
89+
return false;
90+
}
91+
92+
private String convert(int n) {
93+
char[] counter = new char[10];
94+
while (n > 0) {
95+
++counter[n % 10];
96+
n /= 10;
97+
}
98+
return new String(counter);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
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)) return true;
112+
return false;
113+
}
114+
115+
vector<int> convert(int n) {
116+
vector<int> counter(10);
117+
while (n)
118+
{
119+
++counter[n % 10];
120+
n /= 10;
121+
}
122+
return counter;
123+
}
124+
};
125+
```
68126

127+
### **Go**
128+
129+
```go
130+
func reorderedPowerOf2(n int) bool {
131+
convert := func(n int) []byte {
132+
counter := make([]byte, 10)
133+
for n > 0 {
134+
counter[n%10]++
135+
n /= 10
136+
}
137+
return counter
138+
}
139+
140+
s := convert(n)
141+
for i := 1; i <= 1e9; i <<= 1 {
142+
if bytes.Equal(s, convert(i)) {
143+
return true
144+
}
145+
}
146+
return false
147+
}
69148
```
70149

71150
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
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)) return true;
7+
return false;
8+
}
9+
10+
vector<int> convert(int n) {
11+
vector<int> counter(10);
12+
while (n)
13+
{
14+
++counter[n % 10];
15+
n /= 10;
16+
}
17+
return counter;
18+
}
19+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func reorderedPowerOf2(n int) bool {
2+
convert := func(n int) []byte {
3+
counter := make([]byte, 10)
4+
for n > 0 {
5+
counter[n%10]++
6+
n /= 10
7+
}
8+
return counter
9+
}
10+
11+
s := convert(n)
12+
for i := 1; i <= 1e9; i <<= 1 {
13+
if bytes.Equal(s, convert(i)) {
14+
return true
15+
}
16+
}
17+
return false
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean reorderedPowerOf2(int n) {
3+
String s = convert(n);
4+
for (int i = 1; i <= Math.pow(10, 9); i <<= 1) {
5+
if (s.equals(convert(i))) {
6+
return true;
7+
}
8+
}
9+
return false;
10+
}
11+
12+
private String convert(int n) {
13+
char[] counter = new char[10];
14+
while (n > 0) {
15+
++counter[n % 10];
16+
n /= 10;
17+
}
18+
return new String(counter);
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def reorderedPowerOf2(self, n: int) -> bool:
3+
def convert(n):
4+
counter = [0] * 10
5+
while n > 0:
6+
counter[n % 10] += 1
7+
n //= 10
8+
return counter
9+
10+
i, s = 1, convert(n)
11+
while i <= 10 ** 9:
12+
if convert(i) == s:
13+
return True
14+
i <<= 1
15+
return False

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /