Skip to content

Commit 012d1c0

Browse files
committed
一刷869
1 parent aaebef4 commit 012d1c0

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6108,13 +6108,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
61086108
//|{doc_base_url}/0868-binary-gap.adoc[题解]
61096109
//|Easy
61106110
//|
6111-
//
6112-
//|{counter:codes}
6113-
//|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]
6114-
//|{source_base_url}/_0869_ReorderedPowerOf2.java[Java]
6115-
//|{doc_base_url}/0869-reordered-power-of-2.adoc[题解]
6116-
//|Medium
6117-
//|
6111+
6112+
|{counter:codes}
6113+
|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]
6114+
|{source_base_url}/_0869_ReorderedPowerOf2.java[Java]
6115+
|{doc_base_url}/0869-reordered-power-of-2.adoc[题解]
6116+
|Medium
6117+
|
61186118

61196119
|{counter:codes}
61206120
|{leetcode_base_url}/advantage-shuffle/[870. Advantage Shuffle^]

docs/0869-reordered-power-of-2.adoc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ Starting with a positive integer `N`, we reorder the digits in any order (includ
77

88
Return `true` if and only if we can do this in a way such that the resulting number is a power of 2.
99

10-
11-
12-
13-
14-
1510

1611
*Example 1:*
1712

@@ -30,7 +25,6 @@ Return `true` if and only if we can do this in a way such that the resulting num
3025
*Output:* false
3126
----
3227

33-
3428
*Example 3:*
3529

3630
[subs="verbatim,quotes,macros"]
@@ -39,7 +33,6 @@ Return `true` if and only if we can do this in a way such that the resulting num
3933
*Output:* true
4034
----
4135

42-
4336
*Example 4:*
4437

4538
[subs="verbatim,quotes,macros"]
@@ -48,7 +41,6 @@ Return `true` if and only if we can do this in a way such that the resulting num
4841
*Output:* false
4942
----
5043

51-
5244
*Example 5:*
5345

5446
[subs="verbatim,quotes,macros"]
@@ -57,24 +49,40 @@ Return `true` if and only if we can do this in a way such that the resulting num
5749
*Output:* true
5850
----
5951

60-
61-
6252
*Note:*
6353

64-
65-
. `1 <= N <= 10^9`
66-
67-
68-
69-
54+
. `1 \<= N \<= 10^9`
7055

7156

57+
== 思路分析
7258

59+
根据数字,对其对应的字符数组进行排序,然后再取得其对应的字符串。
7360

61+
一次计算 2 的幂次方,然后在字符串长度相等的情况下,排序其对应的字符数组,再转化成字符串跟参数的字符串想比较。
7462

7563
[[src-0869]]
64+
[tabs]
65+
====
66+
一刷::
67+
+
68+
--
7669
[{java_src_attr}]
7770
----
7871
include::{sourcedir}/_0869_ReorderedPowerOf2.java[tag=answer]
7972
----
73+
--
74+
75+
// 二刷::
76+
// +
77+
// --
78+
// [{java_src_attr}]
79+
// ----
80+
// include::{sourcedir}/_0869_ReorderedPowerOf2_2.java[tag=answer]
81+
// ----
82+
// --
83+
====
84+
85+
== 参考资料
86+
87+
. https://leetcode.cn/problems/reordered-power-of-2/solutions/1068761/zhong-xin-pai-xu-de-dao-2-de-mi-by-leetc-4fvs/[869. 重新排序得到 2 的幂 - 官方题解^]
8088

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ include::0796-rotate-string.adoc[leveloffset=+1]
18121812

18131813
// include::0868-binary-gap.adoc[leveloffset=+1]
18141814

1815-
// include::0869-reordered-power-of-2.adoc[leveloffset=+1]
1815+
include::0869-reordered-power-of-2.adoc[leveloffset=+1]
18161816

18171817
include::0870-advantage-shuffle.adoc[leveloffset=+1]
18181818

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@
875875
|⭕️ 贪心。排序规则跟 179、870 题类似:保存下标,使用其他数组的值对下标数组进行排序。
876876

877877

878+
|{counter:codes}
879+
|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]
880+
|{doc_base_url}/0869-reordered-power-of-2.adoc[题解]
881+
|⭕️ 参考答案做了优化
882+
878883
|===
879884

880885
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
6+
public class _0869_ReorderedPowerOf2 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2024-09-25 20:47:35
12+
*/
13+
public boolean reorderedPowerOf2(int n) {
14+
char[] chars = String.valueOf(n).toCharArray();
15+
Arrays.sort(chars);
16+
int pow = 1;
17+
String str = new String(chars);
18+
for (int i = 0; Math.pow(2, i - 1) < Integer.MAX_VALUE; i++) {
19+
String ps = String.valueOf(pow);
20+
if (ps.length() == chars.length) {
21+
char[] pchars = ps.toCharArray();
22+
Arrays.sort(pchars);
23+
if (str.equals(new String(pchars))) {
24+
return true;
25+
}
26+
}
27+
pow *= 2;
28+
}
29+
return false;
30+
}
31+
// end::answer[]
32+
}

0 commit comments

Comments
 (0)