forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheunhwa99.java
20 lines (17 loc) ยท 830 Bytes
/
eunhwa99.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ํ์ด๋ฐฉ๋ฒ
// n์ ๊ฒฝ์ฐ ๋์์๋ถํฐ ๋นํธ์ฐ์ฐ์ ํด์ผ ํ๋ฏ๋ก ๊ฐ์ฅ ์ค๋ฅธ์ชฝ๋ถํฐ ์์ํด์ผ ํจ
// ์ด๋ n๊ณผ 1์ AND ์ฐ์ฐํ๊ณ , n์ ์ค๋ฅธ์ชฝ์ผ๋ก ๋ฏธ๋ฃจ๋ฉด์ ์ํํ๋ฉด ๋๋ค. (n >> 1)
// ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ํ ๊ฐ์ result์ ์ ์ฅํ ์์ ์ด๋ค. result์ ๊ฒฝ์ฐ, n๊ณผ ๋ฐ๋๋ก ์ผ์ชฝ์ผ๋ก ํ ์นธ์ฉ ๋ฏธ๋ฃจ๋ฉด์ n์ ๋นํธ๋ฅผ ์ฝ์
ํด์ค์ผ ํจ (OR ์ฐ์ฐ)
// ์๊ฐ ๋ณต์ก๋ (์ซ์์ ๋นํธ ์ ๋งํผ ํ์) -> O(N) (N: ์ซ์ n์ ๋นํธ ์, ๋ฌธ์ ์์๋ 32๋ก ๊ณ ์ )
// ๊ณต๊ฐ ๋ณต์ก๋: (result ๋ณ์ ํฌ๊ธฐ)
public class Solution {
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result = result << 1;
result |= (n & 1);
n = n >> 1;
}
return result;
}
}