Skip to content

Commit eacc99f

Browse files
committed
add reverse bits solution
1 parent e75a811 commit eacc99f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

reverse-bits/Tessa1217.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
3+
// 비트 연산자 사용
4+
public int reverseBits(int n) {
5+
int answer = 0;
6+
for (int i = 0; i < 32; i++) {
7+
answer <<= 1;
8+
answer |= (n & 1);
9+
n >>>= 1;
10+
}
11+
12+
return answer;
13+
}
14+
15+
// you need treat n as an unsigned value
16+
// O(1)
17+
// public int reverseBits(int n) {
18+
19+
// int answer = 0;
20+
21+
// String binary = Integer.toBinaryString(n);
22+
23+
// StringBuilder padded = new StringBuilder();
24+
// for (int i = 0; i < 32 - binary.length(); i++) {
25+
// padded.append('0');
26+
// }
27+
// padded.append(binary);
28+
29+
// return (int) Long.parseLong(padded.reverse().toString(), 2);
30+
31+
// }
32+
}
33+

0 commit comments

Comments
 (0)