Skip to content

Commit

Permalink
feat: reverse-bits solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YeomChaeeun committed Dec 25, 2024
1 parent 320cd1c commit a9d8a32
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions reverse-bits/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* ์ •์ˆ˜๋ฅผ ๋น„ํŠธ๋กœ ๋ณ€ํ™˜ํ›„ ๋’ค์ง‘์–ด์„œ ๋‹ค์‹œ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
* - ์‹œ๊ฐ„๋ณต์žก๋„: O(1)
* - ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
* @param n
*/
function reverseBits(n: number): number {
// 2์ง„์ˆ˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
let arr = n.toString(2).split('')
let len = arr.length
// 32๋น„ํŠธ ์ •๋ ฌ - ๋ถ€์กฑํ•œ ์•ž์ชฝ์— 0์œผ๋กœ ์ฑ„์›€
for (let i = 0; i < (32 - len); i++) {
arr.unshift('0');
}
// ๋’ค์ง‘์€ ํ›„ ํ•ฉ์นจ
let result = arr.reverse().join('')
// 2์ง„์ˆ˜ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
return parseInt(result,2)
}

0 comments on commit a9d8a32

Please sign in to comment.