Skip to content

Commit 613fa9a

Browse files
committed
Add solution 869
1 parent fe7d984 commit 613fa9a

25 files changed

+597
-291
lines changed

README.md

Lines changed: 225 additions & 217 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
import "fmt"
4+
5+
func reorderedPowerOf2(n int) bool {
6+
sample, i := fmt.Sprintf("%v", n), 1
7+
for len(fmt.Sprintf("%v", i)) <= len(sample) {
8+
t := fmt.Sprintf("%v", i)
9+
if len(t) == len(sample) && isSame(t, sample) {
10+
return true
11+
}
12+
i = i << 1
13+
}
14+
return false
15+
}
16+
17+
func isSame(t, s string) bool {
18+
m := make(map[rune]int)
19+
for _, v := range t {
20+
m[v]++
21+
}
22+
for _, v := range s {
23+
m[v]--
24+
if m[v] < 0 {
25+
return false
26+
}
27+
if m[v] == 0 {
28+
delete(m, v)
29+
}
30+
}
31+
return len(m) == 0
32+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question869 struct {
9+
para869
10+
ans869
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para869 struct {
16+
n int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans869 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem869(t *testing.T) {
26+
27+
qs := []question869{
28+
29+
{
30+
para869{1},
31+
ans869{true},
32+
},
33+
34+
{
35+
para869{10},
36+
ans869{false},
37+
},
38+
39+
{
40+
para869{16},
41+
ans869{true},
42+
},
43+
44+
{
45+
para869{24},
46+
ans869{false},
47+
},
48+
49+
{
50+
para869{46},
51+
ans869{true},
52+
},
53+
54+
{
55+
para869{100},
56+
ans869{false},
57+
},
58+
59+
{
60+
para869{123453242},
61+
ans869{false},
62+
},
63+
}
64+
65+
fmt.Printf("------------------------Leetcode Problem 869------------------------\n")
66+
67+
for _, q := range qs {
68+
_, p := q.ans869, q.para869
69+
fmt.Printf("【input】:%v 【output】:%v\n", p, reorderedPowerOf2(p.n))
70+
}
71+
fmt.Printf("\n\n\n")
72+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [869. Reordered Power of 2](https://leetcode.com/problems/reordered-power-of-2/)
2+
3+
4+
## 题目
5+
6+
Starting with a positive integer `N`, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
7+
8+
Return `true` if and only if we can do this in a way such that the resulting number is a power of 2.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:1
14+
Output:true
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input:10
21+
Output:false
22+
```
23+
24+
**Example 3:**
25+
26+
```
27+
Input:16
28+
Output:true
29+
```
30+
31+
**Example 4:**
32+
33+
```
34+
Input:24
35+
Output:false
36+
```
37+
38+
**Example 5:**
39+
40+
```
41+
Input:46
42+
Output:true
43+
```
44+
45+
**Note:**
46+
47+
1. `1 <= N <= 10^9`
48+
49+
## 题目大意
50+
51+
给定正整数 N ,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。
52+
53+
## 解题思路
54+
55+
- 将整数每个位上的所有排列看成字符串,那么题目转换为判断这些字符串是否和 2 的幂的字符串是否一致。判断的方法有很多种,笔者这里判断借助了一个 `map`。两个不同排列的字符串要相等,所有字符出现的频次必定一样。利用一个 `map` 统计它们各自字符的频次,最终都一致,则判定这两个字符串是满足题意的。
56+
- 此题数据量比较小,在 `[1,10^9]` 这个区间内,2 的幂只有 30 几个,所以最终要判断的字符串就是这 30 几个。笔者这里没有打表了,采用更加一般的做法。数据量更大,此解法代码也能通过。
57+
58+
## 代码
59+
60+
```go
61+
package leetcode
62+
63+
import "fmt"
64+
65+
func reorderedPowerOf2(n int) bool {
66+
sample, i := fmt.Sprintf("%v", n), 1
67+
for len(fmt.Sprintf("%v", i)) <= len(sample) {
68+
t := fmt.Sprintf("%v", i)
69+
if len(t) == len(sample) && isSame(t, sample) {
70+
return true
71+
}
72+
i = i << 1
73+
}
74+
return false
75+
}
76+
77+
func isSame(t, s string) bool {
78+
m := make(map[rune]int)
79+
for _, v := range t {
80+
m[v]++
81+
}
82+
for _, v := range s {
83+
m[v]--
84+
if m[v] < 0 {
85+
return false
86+
}
87+
if m[v] == 0 {
88+
delete(m, v)
89+
}
90+
}
91+
return len(m) == 0
92+
}
93+
```

website/content/ChapterFour/0800~0899/0867.Transpose-Matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ func transpose(A [][]int) [][]int {
6060
----------------------------------------------
6161
<div style="display: flex;justify-content: space-between;align-items: center;">
6262
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys/">⬅️上一页</a></p>
63-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0872.Leaf-Similar-Trees/">下一页➡️</a></p>
63+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0869.Reordered-Power-of-2/">下一页➡️</a></p>
6464
</div>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# [869. Reordered Power of 2](https://leetcode.com/problems/reordered-power-of-2/)
2+
3+
4+
## 题目
5+
6+
Starting with a positive integer `N`, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
7+
8+
Return `true` if and only if we can do this in a way such that the resulting number is a power of 2.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:1
14+
Output:true
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input:10
21+
Output:false
22+
```
23+
24+
**Example 3:**
25+
26+
```
27+
Input:16
28+
Output:true
29+
```
30+
31+
**Example 4:**
32+
33+
```
34+
Input:24
35+
Output:false
36+
```
37+
38+
**Example 5:**
39+
40+
```
41+
Input:46
42+
Output:true
43+
```
44+
45+
**Note:**
46+
47+
1. `1 <= N <= 10^9`
48+
49+
## 题目大意
50+
51+
给定正整数 N ,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。
52+
53+
## 解题思路
54+
55+
- 将整数每个位上的所有排列看成字符串,那么题目转换为判断这些字符串是否和 2 的幂的字符串是否一致。判断的方法有很多种,笔者这里判断借助了一个 `map`。两个不同排列的字符串要相等,所有字符出现的频次必定一样。利用一个 `map` 统计它们各自字符的频次,最终都一致,则判定这两个字符串是满足题意的。
56+
- 此题数据量比较小,在 `[1,10^9]` 这个区间内,2 的幂只有 30 几个,所以最终要判断的字符串就是这 30 几个。笔者这里没有打表了,采用更加一般的做法。数据量更大,此解法代码也能通过。
57+
58+
## 代码
59+
60+
```go
61+
package leetcode
62+
63+
import "fmt"
64+
65+
func reorderedPowerOf2(n int) bool {
66+
sample, i := fmt.Sprintf("%v", n), 1
67+
for len(fmt.Sprintf("%v", i)) <= len(sample) {
68+
t := fmt.Sprintf("%v", i)
69+
if len(t) == len(sample) && isSame(t, sample) {
70+
return true
71+
}
72+
i = i << 1
73+
}
74+
return false
75+
}
76+
77+
func isSame(t, s string) bool {
78+
m := make(map[rune]int)
79+
for _, v := range t {
80+
m[v]++
81+
}
82+
for _, v := range s {
83+
m[v]--
84+
if m[v] < 0 {
85+
return false
86+
}
87+
if m[v] == 0 {
88+
delete(m, v)
89+
}
90+
}
91+
return len(m) == 0
92+
}
93+
```
94+
95+
96+
----------------------------------------------
97+
<div style="display: flex;justify-content: space-between;align-items: center;">
98+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0867.Transpose-Matrix/">⬅️上一页</a></p>
99+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0872.Leaf-Similar-Trees/">下一页➡️</a></p>
100+
</div>

website/content/ChapterFour/0800~0899/0872.Leaf-Similar-Trees.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ func dfsLeaf(root *TreeNode, leaf *[]int) {
6464

6565
----------------------------------------------
6666
<div style="display: flex;justify-content: space-between;align-items: center;">
67-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0867.Transpose-Matrix/">⬅️上一页</a></p>
67+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0869.Reordered-Power-of-2/">⬅️上一页</a></p>
6868
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0875.Koko-Eating-Bananas/">下一页➡️</a></p>
6969
</div>

website/content/ChapterTwo/Array.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ weight: 1
1111
|0001|Two Sum|[Go]({{< relref "/ChapterFour/0001~0099/0001.Two-Sum.md" >}})|Easy| O(n)| O(n)||46.6%|
1212
|0004|Median of Two Sorted Arrays|[Go]({{< relref "/ChapterFour/0001~0099/0004.Median-of-Two-Sorted-Arrays.md" >}})|Hard||||31.2%|
1313
|0011|Container With Most Water|[Go]({{< relref "/ChapterFour/0001~0099/0011.Container-With-Most-Water.md" >}})|Medium| O(n)| O(1)||52.8%|
14-
|0015|3Sum|[Go]({{< relref "/ChapterFour/0001~0099/0015.3Sum.md" >}})|Medium| O(n^2)| O(n)|❤️|28.1%|
14+
|0015|3Sum|[Go]({{< relref "/ChapterFour/0001~0099/0015.3Sum.md" >}})|Medium| O(n^2)| O(n)|❤️|28.2%|
1515
|0016|3Sum Closest|[Go]({{< relref "/ChapterFour/0001~0099/0016.3Sum-Closest.md" >}})|Medium| O(n^2)| O(1)|❤️|46.3%|
1616
|0018|4Sum|[Go]({{< relref "/ChapterFour/0001~0099/0018.4Sum.md" >}})|Medium| O(n^3)| O(n^2)|❤️|35.0%|
1717
|0026|Remove Duplicates from Sorted Array|[Go]({{< relref "/ChapterFour/0001~0099/0026.Remove-Duplicates-from-Sorted-Array.md" >}})|Easy| O(n)| O(1)||46.7%|
@@ -34,7 +34,7 @@ weight: 1
3434
|0059|Spiral Matrix II|[Go]({{< relref "/ChapterFour/0001~0099/0059.Spiral-Matrix-II.md" >}})|Medium| O(n)| O(n^2)||58.0%|
3535
|0062|Unique Paths|[Go]({{< relref "/ChapterFour/0001~0099/0062.Unique-Paths.md" >}})|Medium| O(n^2)| O(n^2)||56.2%|
3636
|0063|Unique Paths II|[Go]({{< relref "/ChapterFour/0001~0099/0063.Unique-Paths-II.md" >}})|Medium| O(n^2)| O(n^2)||35.3%|
37-
|0064|Minimum Path Sum|[Go]({{< relref "/ChapterFour/0001~0099/0064.Minimum-Path-Sum.md" >}})|Medium| O(n^2)| O(n^2)||56.3%|
37+
|0064|Minimum Path Sum|[Go]({{< relref "/ChapterFour/0001~0099/0064.Minimum-Path-Sum.md" >}})|Medium| O(n^2)| O(n^2)||56.4%|
3838
|0066|Plus One|[Go]({{< relref "/ChapterFour/0001~0099/0066.Plus-One.md" >}})|Easy||||42.3%|
3939
|0073|Set Matrix Zeroes|[Go]({{< relref "/ChapterFour/0001~0099/0073.Set-Matrix-Zeroes.md" >}})|Medium||||44.4%|
4040
|0074|Search a 2D Matrix|[Go]({{< relref "/ChapterFour/0001~0099/0074.Search-a-2D-Matrix.md" >}})|Medium||||38.0%|
@@ -53,7 +53,7 @@ weight: 1
5353
|0120|Triangle|[Go]({{< relref "/ChapterFour/0100~0199/0120.Triangle.md" >}})|Medium| O(n^2)| O(n)||45.9%|
5454
|0121|Best Time to Buy and Sell Stock|[Go]({{< relref "/ChapterFour/0100~0199/0121.Best-Time-to-Buy-and-Sell-Stock.md" >}})|Easy| O(n)| O(1)||51.7%|
5555
|0122|Best Time to Buy and Sell Stock II|[Go]({{< relref "/ChapterFour/0100~0199/0122.Best-Time-to-Buy-and-Sell-Stock-II.md" >}})|Easy| O(n)| O(1)||58.7%|
56-
|0126|Word Ladder II|[Go]({{< relref "/ChapterFour/0100~0199/0126.Word-Ladder-II.md" >}})|Hard| O(n)| O(n^2)|❤️|23.8%|
56+
|0126|Word Ladder II|[Go]({{< relref "/ChapterFour/0100~0199/0126.Word-Ladder-II.md" >}})|Hard| O(n)| O(n^2)|❤️|23.9%|
5757
|0128|Longest Consecutive Sequence|[Go]({{< relref "/ChapterFour/0100~0199/0128.Longest-Consecutive-Sequence.md" >}})|Hard||||46.4%|
5858
|0152|Maximum Product Subarray|[Go]({{< relref "/ChapterFour/0100~0199/0152.Maximum-Product-Subarray.md" >}})|Medium| O(n)| O(1)||32.8%|
5959
|0153|Find Minimum in Rotated Sorted Array|[Go]({{< relref "/ChapterFour/0100~0199/0153.Find-Minimum-in-Rotated-Sorted-Array.md" >}})|Medium||||46.3%|
@@ -143,10 +143,10 @@ weight: 1
143143
|1300|Sum of Mutated Array Closest to Target|[Go]({{< relref "/ChapterFour/1300~1399/1300.Sum-of-Mutated-Array-Closest-to-Target.md" >}})|Medium||||43.0%|
144144
|1304|Find N Unique Integers Sum up to Zero|[Go]({{< relref "/ChapterFour/1300~1399/1304.Find-N-Unique-Integers-Sum-up-to-Zero.md" >}})|Easy||||76.7%|
145145
|1313|Decompress Run-Length Encoded List|[Go]({{< relref "/ChapterFour/1300~1399/1313.Decompress-Run-Length-Encoded-List.md" >}})|Easy||||85.4%|
146-
|1329|Sort the Matrix Diagonally|[Go]({{< relref "/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally.md" >}})|Medium||||81.8%|
146+
|1329|Sort the Matrix Diagonally|[Go]({{< relref "/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally.md" >}})|Medium||||81.7%|
147147
|1337|The K Weakest Rows in a Matrix|[Go]({{< relref "/ChapterFour/1300~1399/1337.The-K-Weakest-Rows-in-a-Matrix.md" >}})|Easy||||71.9%|
148148
|1380|Lucky Numbers in a Matrix|[Go]({{< relref "/ChapterFour/1300~1399/1380.Lucky-Numbers-in-a-Matrix.md" >}})|Easy||||70.5%|
149-
|1385|Find the Distance Value Between Two Arrays|[Go]({{< relref "/ChapterFour/1300~1399/1385.Find-the-Distance-Value-Between-Two-Arrays.md" >}})|Easy||||66.4%|
149+
|1385|Find the Distance Value Between Two Arrays|[Go]({{< relref "/ChapterFour/1300~1399/1385.Find-the-Distance-Value-Between-Two-Arrays.md" >}})|Easy||||66.3%|
150150
|1389|Create Target Array in the Given Order|[Go]({{< relref "/ChapterFour/1300~1399/1389.Create-Target-Array-in-the-Given-Order.md" >}})|Easy||||84.9%|
151151
|1423|Maximum Points You Can Obtain from Cards|[Go]({{< relref "/ChapterFour/1400~1499/1423.Maximum-Points-You-Can-Obtain-from-Cards.md" >}})|Medium||||46.8%|
152152
|1437|Check If All 1's Are at Least Length K Places Away|[Go]({{< relref "/ChapterFour/1400~1499/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away.md" >}})|Easy||||62.2%|
@@ -157,8 +157,8 @@ weight: 1
157157
|1512|Number of Good Pairs|[Go]({{< relref "/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md" >}})|Easy||||87.7%|
158158
|1539|Kth Missing Positive Number|[Go]({{< relref "/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number.md" >}})|Easy||||54.9%|
159159
|1608|Special Array With X Elements Greater Than or Equal X|[Go]({{< relref "/ChapterFour/1600~1699/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X.md" >}})|Easy||||61.4%|
160-
|1619|Mean of Array After Removing Some Elements|[Go]({{< relref "/ChapterFour/1600~1699/1619.Mean-of-Array-After-Removing-Some-Elements.md" >}})|Easy||||64.9%|
161-
|1629|Slowest Key|[Go]({{< relref "/ChapterFour/1600~1699/1629.Slowest-Key.md" >}})|Easy||||59.2%|
160+
|1619|Mean of Array After Removing Some Elements|[Go]({{< relref "/ChapterFour/1600~1699/1619.Mean-of-Array-After-Removing-Some-Elements.md" >}})|Easy||||64.8%|
161+
|1629|Slowest Key|[Go]({{< relref "/ChapterFour/1600~1699/1629.Slowest-Key.md" >}})|Easy||||59.1%|
162162
|1636|Sort Array by Increasing Frequency|[Go]({{< relref "/ChapterFour/1600~1699/1636.Sort-Array-by-Increasing-Frequency.md" >}})|Easy||||66.8%|
163163
|1640|Check Array Formation Through Concatenation|[Go]({{< relref "/ChapterFour/1600~1699/1640.Check-Array-Formation-Through-Concatenation.md" >}})|Easy||||59.8%|
164164
|1646|Get Maximum in Generated Array|[Go]({{< relref "/ChapterFour/1600~1699/1646.Get-Maximum-in-Generated-Array.md" >}})|Easy||||53.3%|
@@ -167,9 +167,9 @@ weight: 1
167167
|1672|Richest Customer Wealth|[Go]({{< relref "/ChapterFour/1600~1699/1672.Richest-Customer-Wealth.md" >}})|Easy||||88.2%|
168168
|1700|Number of Students Unable to Eat Lunch|[Go]({{< relref "/ChapterFour/1700~1799/1700.Number-of-Students-Unable-to-Eat-Lunch.md" >}})|Easy||||68.1%|
169169
|1732|Find the Highest Altitude|[Go]({{< relref "/ChapterFour/1700~1799/1732.Find-the-Highest-Altitude.md" >}})|Easy||||80.3%|
170-
|1742|Maximum Number of Balls in a Box|[Go]({{< relref "/ChapterFour/1700~1799/1742.Maximum-Number-of-Balls-in-a-Box.md" >}})|Easy||||74.3%|
171-
|1748|Sum of Unique Elements|[Go]({{< relref "/ChapterFour/1700~1799/1748.Sum-of-Unique-Elements.md" >}})|Easy||||75.2%|
172-
|1752|Check if Array Is Sorted and Rotated|[Go]({{< relref "/ChapterFour/1700~1799/1752.Check-if-Array-Is-Sorted-and-Rotated.md" >}})|Easy||||62.3%|
170+
|1742|Maximum Number of Balls in a Box|[Go]({{< relref "/ChapterFour/1700~1799/1742.Maximum-Number-of-Balls-in-a-Box.md" >}})|Easy||||74.4%|
171+
|1748|Sum of Unique Elements|[Go]({{< relref "/ChapterFour/1700~1799/1748.Sum-of-Unique-Elements.md" >}})|Easy||||75.1%|
172+
|1752|Check if Array Is Sorted and Rotated|[Go]({{< relref "/ChapterFour/1700~1799/1752.Check-if-Array-Is-Sorted-and-Rotated.md" >}})|Easy||||62.2%|
173173
|1758|Minimum Changes To Make Alternating Binary String|[Go]({{< relref "/ChapterFour/1700~1799/1758.Minimum-Changes-To-Make-Alternating-Binary-String.md" >}})|Easy||||58.6%|
174174
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
175175

0 commit comments

Comments
 (0)