forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleehyeyun.js
More file actions
65 lines (52 loc) ยท 1.59 KB
/
leehyeyun.js
File metadata and controls
65 lines (52 loc) ยท 1.59 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @param {number} n
* @return {number}
*/
/*
๋ฌธ์ ์ค๋ช
:
์ฃผ์ด์ง 32๋นํธ ๋ถํธ ์๋ ์ ์ n์ ๋นํธ ์์๋ฅผ ๋ค์ง๋ ๋ฌธ์ ์ด๋ค.
n์ ํญ์ 32๋นํธ ์ด์ง์๋ก ์ทจ๊ธ๋๋ฉฐ,
๊ฐ์ฅ ์ค๋ฅธ์ชฝ ๋นํธ๋ ๊ฐ์ฅ ์ผ์ชฝ์ผ๋ก,
๊ฐ์ฅ ์ผ์ชฝ ๋นํธ๋ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๋ค.
์ฆ, 32๊ฐ์ ๋นํธ ์ ์ฒด๋ฅผ ๊ธฐ์ค์ผ๋ก ์์๋ฅผ ์์ ํ ๋ฐ์ ์ํจ๋ค.
์ด๋ ์ซ์์ ํฌ๊ธฐ๋ ๋ถํธ๊ฐ ์๋๋ผ,
๊ณ ์ ๋ 32๋นํธ ์ด์ง ํํ ์์ฒด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฒ๋ฆฌํด์ผ ํ๋ฉฐ
์์ชฝ์ ์๋ 0 ๋นํธ๋ ๋ฐ๋์ ํฌํจ๋๋ค.
์
๋ ฅ:
- n: 32๋นํธ ๋ถํธ ์๋ ์ ์
- 0 โค n โค 2ยณยน โ 2
- n์ ํญ์ ์ง์์ด๋ค.
์ถ๋ ฅ:
- n์ 32๋นํธ ์ด์ง ํํ์ ๋ค์ง์ ๊ฐ์ ์ ์๋ก ๋ฐํํ๋ค.
์์ 1:
์
๋ ฅ:
n = 43261596
์ด์ง ํํ:
00000010100101000001111010011100
๋นํธ ์์ ๋ฐ์ :
00111001011110000010100101000000
์ถ๋ ฅ:
964176192
์์ 2:
์
๋ ฅ:
n = 2147483644
์ด์ง ํํ:
01111111111111111111111111111100
๋นํธ ์์ ๋ฐ์ :
00111111111111111111111111111110
์ถ๋ ฅ:
1073741822
์ถ๊ฐ ์กฐ๊ฑด (Follow up):
- ์ด ํจ์๊ฐ ๋งค์ฐ ์์ฃผ ํธ์ถ๋๋ ์ํฉ์ ๊ฐ์ ํ๊ณ ,
์ฑ๋ฅ์ ์ต์ ํํ ์ ์๋ ๋ฐฉ๋ฒ์ ๊ณ ๋ คํด์ผ ํ๋ค.
*/
var reverseBits = function(n) {
const binaryString = n.toString(2).padStart(32, "0");
const splitString = binaryString.toString().split("");
const reverseArray = splitString.reverse();
const joinArray = reverseArray.join("");
const value = parseInt(joinArray, 2);
return value;
};
console.log(reverseBits(43261596))
console.log(reverseBits(2147483644))