Skip to content

Commit bcc75cb

Browse files
committed
一刷231
1 parent 768b854 commit bcc75cb

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,13 +1643,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
16431643
|Medium
16441644
|
16451645

1646-
//|{counter:codes}
1647-
//|{leetcode_base_url}/power-of-two/[231. Power of Two^]
1648-
//|{source_base_url}/_0231_PowerOfTwo.java[Java]
1649-
//|{doc_base_url}/0231-power-of-two.adoc[题解]
1650-
//|Easy
1651-
//|
1652-
//
1646+
|{counter:codes}
1647+
|{leetcode_base_url}/power-of-two/[231. Power of Two^]
1648+
|{source_base_url}/_0231_PowerOfTwo.java[Java]
1649+
|{doc_base_url}/0231-power-of-two.adoc[题解]
1650+
|Easy
1651+
|
1652+
16531653
//|{counter:codes}
16541654
//|{leetcode_base_url}/implement-queue-using-stacks/[232. Implement Queue using Stacks^]
16551655
//|{source_base_url}/_0232_ImplementQueueUsingStacks.java[Java]

docs/0231-power-of-two.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,38 @@ Given an integer, write a function to determine if it is a power of two.
3232
*Output:* false
3333
----
3434

35+
== 思路分析
3536

37+
这道题可以利用 xref:0191-number-of-1-bits.adoc[191. Number of 1 Bits] 中的思路。
38+
39+
2 的幂次方数,它们的二进制数只有一个 `1`,那么 `n & (n - 1)` 必然为 `0`。
40+
41+
看答案,也可以使用约数法。
3642

3743
[[src-0231]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
3849
[{java_src_attr}]
3950
----
4051
include::{sourcedir}/_0231_PowerOfTwo.java[tag=answer]
4152
----
53+
--
54+
55+
// 二刷::
56+
// +
57+
// --
58+
// [{java_src_attr}]
59+
// ----
60+
// include::{sourcedir}/_0231_PowerOfTwo_2.java[tag=answer]
61+
// ----
62+
// --
63+
====
64+
65+
== 参考资料
66+
67+
. https://leetcode.cn/problems/power-of-two/solutions/796201/2de-mi-by-leetcode-solution-rny3/?envType=study-plan-v2&envId=selected-coding-interview[231. 2 的幂 - 官方题解^]
68+
. https://leetcode.cn/problems/number-of-1-bits/solutions/2361978/191-wei-1-de-ge-shu-wei-yun-suan-qing-xi-40rw/?envType=study-plan-v2&envId=selected-coding-interview[191. 位1的个数 - 位运算,清晰图解^]
4269

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ include::0227-basic-calculator-ii.adoc[leveloffset=+1]
536536

537537
include::0230-kth-smallest-element-in-a-bst.adoc[leveloffset=+1]
538538

539-
// include::0231-power-of-two.adoc[leveloffset=+1]
539+
include::0231-power-of-two.adoc[leveloffset=+1]
540540

541541
// include::0232-implement-queue-using-stacks.adoc[leveloffset=+1]
542542

logbook/202406.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,12 @@
590590
|{doc_base_url}/0240-search-a-2d-matrix-ii.adoc[题解]
591591
|不能对矩阵做二分查找,可以对单行做二分查找。要分析题目,查看如何排除不符合要求的元素。
592592

593+
|{counter:codes}
594+
|{leetcode_base_url}/power-of-two/[231. Power of Two^]
595+
|{doc_base_url}/0231-power-of-two.adoc[题解]
596+
|位运算, `n & (n - 1)` 或约数
597+
598+
593599
|===
594600

595601
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0231_PowerOfTwo {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-15 20:19:55
8+
*/
9+
public boolean isPowerOfTwo(int n) {
10+
return n > 0 && (n & (n - 1)) == 0;
11+
}
12+
// end::answer[]
13+
}

0 commit comments

Comments
 (0)