File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ์ ์๋ฅผ 32๋นํธ๋ก ๋ณํํ๊ณ ๋ฐ์ ์์ผ ๊ทธ๋์ ์ ์๋ฅผ ๋ฐํํ๋ ๋ฌธ์ .
3
+ *
4
+ * @param {number } n - ์ ์ (32๋นํธ))
5
+ * @returns {number } - 2์ง์ ๋ณํ ๋ฐ ๋ฐ์ ํ์ฌ ์ ์ ๋ณํ.
6
+ *
7
+ * ๋ด์ฅ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ 32๋นํธ 2์ง์ ๋ณํ ํ, reverseํ์ฌ ๋ค์ ์ ์๋ก ๋ณํ.
8
+ *
9
+ * ์๊ฐ ๋ณต์ก๋: O(32)
10
+ * - 32๋นํธ ์ ์์ ๋นํธ๋ฅผ ์ฒ๋ฆฌํ๋ฏ๋ก ๊ณ ์ ๋ ์์ ์๊ฐ.
11
+ *
12
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(32)
13
+ * - 2์ง์ ๋ฌธ์์ด์ ์์ฑํ๊ณ ๋ฐ์ ๋ ๋ฌธ์์ด์ ์ ์ฅํ๋ฏ๋ก ๊ณ ์ ๋ ํฌ๊ธฐ์ ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์.
14
+ */
15
+ function reverseBits ( n : number ) : number {
16
+ // ์ซ์๋ฅผ 32๋นํธ 2์ง์ ๋ฌธ์์ด๋ก ๋ณํ (์์ 0 ์ฑ์ฐ๊ธฐ)
17
+ const binaryStr = n . toString ( 2 ) . padStart ( 32 , '0' ) ;
18
+ // 2์ง์ ๋ฌธ์์ด์ ๋ค์ง๊ธฐ
19
+ const reversedBinaryStr = binaryStr . split ( '' ) . reverse ( ) . join ( '' ) ;
20
+ // ๋ค์งํ 2์ง์ ๋ฌธ์์ด์ ๋ค์ ์ซ์๋ก ๋ณํ
21
+ return parseInt ( reversedBinaryStr , 2 ) ;
22
+ } ;
You canโt perform that action at this time.
0 commit comments