Skip to content

Commit 7b9d6ff

Browse files
committed
reverse bits
1 parent 8836835 commit 7b9d6ff

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

reverse-bits/eunhwa99.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 풀이방법
2+
// n의 경우 끝에서부터 비트연산을 해야 하므로 가장 오른쪽부터 시작해야 함
3+
// 이는 n과 1을 AND 연산하고, n을 오른쪽으로 미루면서 수행하면 된다. (n >> 1)
4+
// 최종 결과를 위한 값은 result에 저장할 예정이다. result의 경우, n과 반대로 왼쪽으로 한 칸씩 미루면서 n의 비트를 삽입해줘야 함 (OR 연산)
5+
6+
// 시간 복잡도 (숫자의 비트 수 만큼 필요) -> O(N) (N: 숫자 n의 비트 수, 문제에서는 32로 고정)
7+
// 공간 복잡도: (result 변수 크기)
8+
public class Solution {
9+
10+
public int reverseBits(int n) {
11+
int result = 0;
12+
for (int i = 0; i < 32; i++) {
13+
result = result << 1;
14+
result |= (n & 1);
15+
n = n >> 1;
16+
}
17+
return result;
18+
}
19+
}

0 commit comments

Comments
 (0)