Skip to content

Commit 9d5a7d5

Browse files
authored
Merge pull request #783 from 0xff-dev/869
Add solution and test-cases for problem 869
2 parents 54e0718 + 588a076 commit 9d5a7d5

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

leetcode/801-900/0869.Reordered-Power-of-2/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [869.Reordered Power of 2][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
5+
6+
Return `true` if and only if we can do this so that the resulting number is a power of two.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 1
12+
Output: true
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Reordered Power of 2
23-
```go
2417
```
25-
18+
Input: n = 10
19+
Output: false
20+
```
2621

2722
## 结语
2823

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func toKey(n int) [10]int {
4+
tmp := [10]int{}
5+
for n > 0 {
6+
cur := n % 10
7+
tmp[cur]++
8+
n /= 10
9+
}
10+
return tmp
11+
}
12+
func Solution(n int) bool {
13+
if n&(n-1) == 0 {
14+
return true
15+
}
16+
17+
cache := map[[10]int]struct{}{}
18+
key := toKey(n)
19+
end := 0
20+
for i := 9; i >= 0; i-- {
21+
count := key[i]
22+
for ; count > 0; count-- {
23+
end = end*10 + i
24+
}
25+
}
26+
shift := 0
27+
cur := 1
28+
for cur <= end {
29+
t := toKey(cur)
30+
cache[t] = struct{}{}
31+
shift++
32+
cur = 1 << shift
33+
}
34+
_, ok := cache[toKey(n)]
35+
return ok
536
}

leetcode/801-900/0869.Reordered-Power-of-2/Solution_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, true},
17+
{"TestCase2", 10, false},
18+
{"TestCase3", 46, true},
19+
{"TestCase4", 125, true},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)