Skip to content

Commit df919d5

Browse files
committed
feat: [Week 08-5] solve sum of two integers
1 parent daa1641 commit df919d5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

sum-of-two-integers/Chaedie.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Solution: 해설을 활용한 풀이입니다.
3+
1) xor 로 더하기를 만들 수있다.
4+
2) & << 1 로 carry 를 만들 수 있다.
5+
3) python 의 경우 32비트 마스크를 사용해 음수 케이스를 고려한다.
6+
7+
"""
8+
9+
10+
class Solution:
11+
def getSum(self, a: int, b: int) -> int:
12+
mask = 0xFFFFFFFF
13+
14+
xor = a ^ b
15+
carry = (a & b) << 1
16+
while carry & mask:
17+
xor, carry = xor ^ carry, (xor & carry) << 1
18+
return (xor & mask) if carry > 0 else xor

0 commit comments

Comments
 (0)