We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e75a811 commit eacc99fCopy full SHA for eacc99f
reverse-bits/Tessa1217.java
@@ -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