File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ *@link https://leetcode.com/problems/sum-of-two-integers/description/
3
+ *
4
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
5
+ * - ๋นํธ AND ์ฐ์ฐ์(&) ์ฌ์ฉํด์ ์๋ฆฌ ์ฌ๋ฆผ์ด ํ์ํ ๋นํธ ๊ณ์ฐํ๊ณ ์ผ์ชฝ ์ํธํธ๋ก ์๋ฆฌ ์ฌ๋ฆผ๊ฐ์ ๋ค์ ์๋ฆฌ๋ก ์ด๋
6
+ * - ๋นํธ XOR ์ฐ์ฐ์(^) ์ฌ์ฉํด์ ์๋ฆฌ ์ฌ๋ฆผ ์ ์ธํ ์๋ฆฌํฉ ๊ณ์ฐํ๊ณ a๊ฐ ์
๋ฐ์ดํธ
7
+ * - ์๋ฆฌ ์ฌ๋ฆผ๊ฐ์ด 0์ด ๋ ๋๊น์ง ์๋ฆฌ ์ฌ๋ฆผ ๋ฐ๋ณต
8
+ * - ์๋ฆฌ ์ฌ๋ฆผ ์์ผ๋ฉด ์ต์ข
ํฉ์ด ์ ์ฅ๋ a ๋ฆฌํด
9
+ *
10
+ *
11
+ * ์๊ฐ๋ณต์ก๋ : O(k)
12
+ * - k๋ ์ซ์์ ๋นํธ ์, ์ต๋ k๋ฒ ๋ฐ๋ณต
13
+ *
14
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(1)
15
+ * - ๊ณ ์ ๋ ๋ณ์๋ง ์ฌ์ฉ
16
+ *
17
+ */
18
+
19
+ function getSum ( a : number , b : number ) : number {
20
+ while ( b ) {
21
+ // ์๋ฆฌ ์ฌ๋ฆผ ๊ณ์ฐ
22
+ const carry = ( a & b ) << 1 ;
23
+ // ์๋ฆฌํฉ ์
๋ฐ์ดํธ (๊ฐ์ ๋นํธ = 0, ๋ค๋ฅธ ๋นํธ = 1)
24
+ a ^= b ;
25
+ b = carry ;
26
+ }
27
+
28
+ return a ;
29
+ }
You canโt perform that action at this time.
0 commit comments