Skip to content

Commit 26b6991

Browse files
committed
[Week3](gmlwls96) reverse-bits
1 parent 471cb9d commit 26b6991

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

reverse-bits/gmlwls96.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
// you need treat n as an unsigned value
3+
fun reverseBits(n: Int): Int {
4+
var bitString = Integer.toBinaryString(n)
5+
bitString = CharArray(32 - bitString.length) { '0' }.concatToString() + bitString
6+
var result = 0
7+
var scale = 1
8+
bitString.forEach {
9+
result += it.digitToInt() * scale
10+
scale *= 2
11+
}
12+
return result
13+
}
14+
}

0 commit comments

Comments
 (0)