Skip to content

Commit acf9053

Browse files
committed
sadie100: reverse bits solution
1 parent f08f147 commit acf9053

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

reverse-bits/sadie100.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
n의 뒤의 자리에서부터 비트로 변환해서 result에 붙이고, result 비트를 앞으로 당기는 일을 32번 반복해서 뒤집어진 비트를 만든다
3+
4+
시간복잡도 : O(1) - 비트 연산
5+
공간복잡도 : O(1)
6+
*/
7+
8+
function reverseBits(n: number): number {
9+
let result = 0
10+
11+
for (let i = 0; i < 32; i++) {
12+
let bit = n & 1
13+
14+
result = (result << 1) | bit
15+
16+
n = n >>> 1
17+
}
18+
return result >>> 0
19+
}

0 commit comments

Comments
 (0)