Skip to content

Commit

Permalink
reverse bits solved
Browse files Browse the repository at this point in the history
  • Loading branch information
mintheon committed Dec 27, 2024
1 parent 79627ed commit 5747260
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions reverse-bits/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
/**
์‹œ๊ฐ„๋ณต์žก๋„: O(1) -> ๋ฃจํ”„๋Š” ํ•ญ์ƒ 32๋ฒˆ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ
๊ณต๊ฐ„๋ณต์žก๋„: O(1)
*/

// you need treat n as an unsigned value
public int reverseBits(int n) {
int answer = 0;
int index = 31;

while(n != 0) {
// n&1 : ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๋ฅผ ์ถ”์ถœ
// << : 0์„ ํŒจ๋”ฉ์ฒ˜๋ฆฌ ์‹œ์ผœ์„œ ์ƒ์œ„ ์ž๋ฆฌ์ˆ˜๋กœ ์˜ฌ๋ ค๋ฒ„๋ฆผ
answer += (n & 1) << index;

// >>> : ๋ถ€ํ˜ธ ์ƒ๊ด€์—†์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋น„ํŠธ ์ด๋™
n = n >>> 1;
index--;
}

return answer;
}
}

0 comments on commit 5747260

Please sign in to comment.