File tree Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Original file line number Diff line number Diff line change 3
3
- -1000 <= a, b <= 1000
4
4
5
5
Time Complexity: O(1)
6
+ - ๋นํธ ์๋ฆฌ์, while loop ๋ฐ๋ณต ํ์๊ฐ ๊ณ ์ ๋์ด ์์
6
7
7
8
Space Complexity: O(1)
8
9
- ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์
๋ ฅ๋ฐ์ ๋ณ์๋ง ์ฌ์ฉ
11
12
1. XOR(^)์ฐ์ฐ์ ํตํด ์บ๋ฆฌ๋ฅผ ์ ์ธํ ๊ฐ ์๋ฆฌ์ ํฉ์ ๊ตฌํจ
12
13
2. AND(&)์ฐ์ฐ ํ ์ผ์ชฝ ์ํํธ(<<)๋ก ๋ค์ ์๋ฆฌ๋ก ์ฌ๋ผ๊ฐ ์บ๋ฆฌ๋ฅผ ๊ตฌํจ
13
14
3. ์บ๋ฆฌ๊ฐ 0์ด ๋ ๋๊น์ง 1-2 ๊ณผ์ ์ ๋ฐ๋ณต
15
+ - ์บ๋ฆฌ๋ ์ ํํ ๋นํธ ๋ฒ์์์๋ง ์กด์ฌํจ
16
+ - ๊ทธ๋ฌ๋ฏ๋ก ๊ณ์ ์ผ์ชฝ์ผ๋ก ์ด๋ํ๋ค ๊ฒฐ๊ตญ ์ฌ๋ผ์ง
17
+ - ์บ๋ฆฌ๊ฐ 0์ด ๋๋ฉด ๋ฃจํ ์ข
๋ฃ
14
18
"""
15
19
# Solution 1: ์ดํดํ๊ธฐ ์ฌ์ด ๋ฒ์
16
20
class Solution :
17
21
def getSum (self , a : int , b : int ) -> int :
18
22
while b :
23
+ # XOR: ์ด๋์ ์บ๋ฆฌ๊ฐ ๋ฐ์ํ๋์ง ํ์ธ
19
24
current_sum = a ^ b
20
25
26
+ # AND: ์บ๋ฆฌ๋ฅผ ๋ค์ ์๋ฆฌ๋ก ์ฌ๋ฆผ
21
27
next_carry = (a & b ) << 1
22
28
23
29
a = current_sum
You canโt perform that action at this time.
0 commit comments