File tree Expand file tree Collapse file tree 3 files changed +74
-31
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 3 files changed +74
-31
lines changed Original file line number Diff line number Diff line change 11[#0231-power-of-two]
2- = 231. Power of Two
2+ = 231. 2 的幂
33
4- { leetcode} /problems/power-of-two/[LeetCode - Power of Two ^]
4+ https:// leetcode.cn /problems/power-of-two/[LeetCode - 231. 2 的幂 ^]
55
6- Given an integer, write a function to determine if it is a power of two.
6+ 给你一个整数 `n` ,请你判断该整数是否是 2 的幂次方。如果是,返回 `true` ;否则,返回 `false` 。
77
8- *Example 1:*
8+ 如果存在一个整数 `x` 使得 `n == 2^x^` ,则认为 `n` 是 `2` 的幂次方。
99
10- [subs="verbatim,quotes,macros"]
11- ----
12- *Input:* 1
13- *Output:* true
14- *Explanation:* 2^0^ = 1
10+ *示例 1:*
1511
16- ----
12+ ....
13+ 输入:n = 1
14+ 输出:true
15+ 解释:20 = 1
16+ ....
1717
18- *Example 2: *
18+ *示例 2: *
1919
20- [subs="verbatim,quotes,macros"]
21- ----
22- *Input:* 16
23- *Output:* true
24- *Explanation:* 2^4^ = 16
25- ----
20+ ....
21+ 输入:n = 16
22+ 输出:true
23+ 解释:24 = 16
24+ ....
2625
27- *Example 3: *
26+ *示例 3: *
2827
29- [subs="verbatim,quotes,macros"]
30- ----
31- *Input:* 218
32- *Output:* false
33- ----
28+ ....
29+ 输入:n = 3
30+ 输出:false
31+ ....
32+
33+ *提示:*
34+
35+ * `-2^31^ \<= n \<= 2^31^ - 1`
36+
37+ **进阶:** 你能够不使用循环/递归解决此问题吗?
3438
3539== 思路分析
3640
@@ -52,14 +56,14 @@ include::{sourcedir}/_0231_PowerOfTwo.java[tag=answer]
5256----
5357--
5458
55- // 二刷::
56- // +
57- // --
58- // [{java_src_attr}]
59- // ----
60- // include::{sourcedir}/_0231_PowerOfTwo_2.java[tag=answer]
61- // ----
62- // --
59+ 二刷::
60+ +
61+ --
62+ [{java_src_attr}]
63+ ----
64+ include::{sourcedir}/_0231_PowerOfTwo_2.java[tag=answer]
65+ ----
66+ --
6367====
6468
6569== 参考资料
Original file line number Diff line number Diff line change @@ -175,6 +175,11 @@ endif::[]
175175|{doc_base_url} /0046-permutations.adoc[题解]
176176|✅ 回溯
177177
178+ |{counter:codes2503}
179+ |{leetcode_base_url} /power-of-two/[231. 2 的幂^]
180+ |{doc_base_url} /0231-power-of-two.adoc[题解]
181+ |✅ 更巧妙的解法是位运算,如果 `n` 是 `2` 的幂,则二进制只有第一位是 `1` ,减一则二进制都是 `1` ,相与 `n & (n - 1)` 则为 `0` 。
182+
178183|===
179184
180185截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change 1+ package com .diguage .algo .leetcode ;
2+
3+ public class _0231_PowerOfTwo_2 {
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+ if (n <= 0 ) {
11+ return false ;
12+ }
13+ if (n == 1 || n == 2 ) {
14+ return true ;
15+ }
16+ if (n % 2 == 1 ) {
17+ return false ;
18+ }
19+ // 注意:这里必须用 long,否则会溢出导致 pow 成为一个更小的数字
20+ long pow = 2 ;
21+ while (pow < n ) {
22+ if (pow * pow <= n ) {
23+ pow = pow * pow ;
24+ } else {
25+ pow *= 2 ;
26+ }
27+ }
28+ return pow == n ;
29+ }
30+ // end::answer[]
31+ public static void main (String [] args ) {
32+ new _0231_PowerOfTwo_2 ().isPowerOfTwo (131072 );
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments