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 33- -1000 <= a, b <= 1000
44
55Time Complexity: O(1)
6+ - ๋นํธ ์๋ฆฌ์, while loop ๋ฐ๋ณต ํ์๊ฐ ๊ณ ์ ๋์ด ์์
67
78Space Complexity: O(1)
89- ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์
๋ ฅ๋ฐ์ ๋ณ์๋ง ์ฌ์ฉ
11121. XOR(^)์ฐ์ฐ์ ํตํด ์บ๋ฆฌ๋ฅผ ์ ์ธํ ๊ฐ ์๋ฆฌ์ ํฉ์ ๊ตฌํจ
12132. AND(&)์ฐ์ฐ ํ ์ผ์ชฝ ์ํํธ(<<)๋ก ๋ค์ ์๋ฆฌ๋ก ์ฌ๋ผ๊ฐ ์บ๋ฆฌ๋ฅผ ๊ตฌํจ
13143. ์บ๋ฆฌ๊ฐ 0์ด ๋ ๋๊น์ง 1-2 ๊ณผ์ ์ ๋ฐ๋ณต
15+ - ์บ๋ฆฌ๋ ์ ํํ ๋นํธ ๋ฒ์์์๋ง ์กด์ฌํจ
16+ - ๊ทธ๋ฌ๋ฏ๋ก ๊ณ์ ์ผ์ชฝ์ผ๋ก ์ด๋ํ๋ค ๊ฒฐ๊ตญ ์ฌ๋ผ์ง
17+ - ์บ๋ฆฌ๊ฐ 0์ด ๋๋ฉด ๋ฃจํ ์ข
๋ฃ
1418"""
1519# Solution 1: ์ดํดํ๊ธฐ ์ฌ์ด ๋ฒ์
1620class Solution :
1721 def getSum (self , a : int , b : int ) -> int :
1822 while b :
23+ # XOR: ์ด๋์ ์บ๋ฆฌ๊ฐ ๋ฐ์ํ๋์ง ํ์ธ
1924 current_sum = a ^ b
2025
26+ # AND: ์บ๋ฆฌ๋ฅผ ๋ค์ ์๋ฆฌ๋ก ์ฌ๋ฆผ
2127 next_carry = (a & b ) << 1
2228
2329 a = current_sum
You canโt perform that action at this time.
0 commit comments