forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoonDongKang.ts
More file actions
37 lines (33 loc) · 862 Bytes
/
HoonDongKang.ts
File metadata and controls
37 lines (33 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* [Problem]: [190] Reverse Bits
* (https://leetcode.com/problems/reverse-bits/description/)
*/
function reverseBits(n: number): number {
//시간복잡도 O(1)
//공간복잡도 O(1)
function stackFunc(n: number): number {
const stack: number[] = [];
let output = 0;
let scale = 1;
while (stack.length < 32) {
stack.push(n % 2);
n = Math.floor(n / 2);
}
while (stack.length) {
output += stack.pop()! * scale;
scale *= 2;
}
return output;
}
//시간복잡도 O(1)
//공간복잡도 O(1)
function bitFunc(n: number): number {
let result = 0;
for (let i = 0; i < 32; i++) {
result <<= 1;
result |= n & 1;
n >>>= 1;
}
return result >>> 0;
}
}