- Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPower-Of-Two.kt
61 lines (58 loc) · 1.19 KB
/
Power-Of-Two.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
classPowerofTwoKotlin231 {
companionobject {
val resultSet =setOf(
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
32768,
65536,
131072,
262144,
524288,
1048576,
2097152,
4194304,
8388608,
16777216,
33554432,
67108864,
134217728,
268435456,
536870912,
1073741824
)
}
funisPowerOfTwo(n:Int): Boolean {
return resultSet.contains(n)
}
/*
fun isPowerOfTwo(n: Int): Boolean {
if (n == 1) {
return true
}
var current = 2
while (current < n && current <= 1073741823) {
current = current.shl(1)
}
return current == n
}
*/
}
funmain() {
val solution =PowerofTwoKotlin231()
println(solution.isPowerOfTwo(4))
println(solution.isPowerOfTwo(1073741825))
println(solution.isPowerOfTwo(2))
}