Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbdddebd

Browse files
authored
Create 0231-power-of-two.kt
1 parentfe62c16 commitbdddebd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎kotlin/0231-power-of-two.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// iterative
2+
classSolution {
3+
funisPowerOfTwo(n:Int):Boolean {
4+
if (n==0)returnfalse
5+
if (n==1)returntrue
6+
7+
var n= n
8+
while (n%2==0)
9+
n= n shr1
10+
11+
return n==1
12+
}
13+
}
14+
15+
// recursive
16+
classSolution {
17+
funisPowerOfTwo(n:Int):Boolean {
18+
if (n==1)returntrue
19+
if (n<=0|| n%2!=0)returnfalse
20+
return isPowerOfTwo(n shr1)
21+
}
22+
}
23+
24+
// one line bit manipulation
25+
classSolution {
26+
funisPowerOfTwo(n:Int)= (n>0)&& (nand (n-1)==0)
27+
}
28+
29+
// one line bit manipulation
30+
classSolution {
31+
funisPowerOfTwo(n:Int)= (n>0&& ((1 shl30)% n)==0)
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp