forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjuhui-jeong.java
More file actions
42 lines (37 loc) ยท 1.01 KB
/
juhui-jeong.java
File metadata and controls
42 lines (37 loc) ยท 1.01 KB
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
/*
* ์๊ฐ ๋ณต์ก๋: O(1)
* ๊ณต๊ฐ ๋ณต์ก๋: O(1)
*/
class Solution {
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>= 1;
}
return result;
}
}
/*
* ์๊ฐ ๋ณต์ก๋: O(1)
* ๊ณต๊ฐ ๋ณต์ก๋: O(32)
*
* ํด๋น ์ฝ๋๋ก๋ ๋์ํ์ง๋ง ๋ฌธ์์ด ๋ณํ -> ๋ค์ง๊ธฐ -> ๋ค์ ๋ฌธ์์ด ํ์ฑ์ ๊ณผ์ ์ ๊ฑฐ์น์ง ์๊ณ
* ๋นํธ๋ฅผ ์กฐ์ํ๋ ๊ฒ์ด ๋ฌธ์ ์ ์๋์ ๋ ๋ถํฉํจ.
*
class Solution {
public static String toBinaryString(int value) {
String str = Integer.toBinaryString(value);
while (str.length() < 32) {
str = "0" + str;
}
return str;
}
public int reverseBits(int n) {
String binaryString = toBinaryString(n);
String reversed = new StringBuilder(binaryString).reverse().toString();
int result = Integer.parseUnsignedInt(reversed, 2);
return result;
}
}
*/