File tree 1 file changed +61
-0
lines changed
June-LeetCoding-Challenge/08-Power-Of-Two
1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class PowerofTwoKotlin231 {
2
+
3
+ companion object {
4
+ val resultSet = setOf (
5
+ 1 ,
6
+ 2 ,
7
+ 4 ,
8
+ 8 ,
9
+ 16 ,
10
+ 32 ,
11
+ 64 ,
12
+ 128 ,
13
+ 256 ,
14
+ 512 ,
15
+ 1024 ,
16
+ 2048 ,
17
+ 4096 ,
18
+ 8192 ,
19
+ 16384 ,
20
+ 32768 ,
21
+ 65536 ,
22
+ 131072 ,
23
+ 262144 ,
24
+ 524288 ,
25
+ 1048576 ,
26
+ 2097152 ,
27
+ 4194304 ,
28
+ 8388608 ,
29
+ 16777216 ,
30
+ 33554432 ,
31
+ 67108864 ,
32
+ 134217728 ,
33
+ 268435456 ,
34
+ 536870912 ,
35
+ 1073741824
36
+ )
37
+ }
38
+
39
+ fun isPowerOfTwo (n : Int ): Boolean {
40
+ return resultSet.contains(n)
41
+ }
42
+ /*
43
+ fun isPowerOfTwo(n: Int): Boolean {
44
+ if (n == 1) {
45
+ return true
46
+ }
47
+ var current = 2
48
+ while (current < n && current <= 1073741823) {
49
+ current = current.shl(1)
50
+ }
51
+ return current == n
52
+ }
53
+ */
54
+ }
55
+
56
+ fun main () {
57
+ val solution = PowerofTwoKotlin231 ()
58
+ println (solution.isPowerOfTwo(4 ))
59
+ println (solution.isPowerOfTwo(1073741825 ))
60
+ println (solution.isPowerOfTwo(2 ))
61
+ }
You can’t perform that action at this time.
0 commit comments