We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2e26787 commit 43054fdCopy full SHA for 43054fd
sum-of-two-integers/yyyyyyyyyKim.py
@@ -0,0 +1,19 @@
1
+class Solution:
2
+ def getSum(self, a: int, b: int) -> int:
3
+
4
+ # 비트 연산(보완이 필요함, python은 무제한정수를 사용하므로 mask써서 32bit 정수로 잘라줘야함)
5
+ mask = 0xFFFFFFFF # 32비트 정수 마스크
6
+ max_int = 0X7FFFFFFF # 양수 최대값
7
8
+ while b != 0:
9
+ # 자리올림(AND연산)
10
+ carry = (a&b) & mask
11
+ # 자리올림없이 더하기(XOR연산)
12
+ a = (a^b) & mask
13
+ # 왼쪽으로 1 비트이동(다음 자리에서 더할 carry값)
14
+ b = (carry << 1) & mask
15
16
+ # a가 음수인 경우 보수 변환
17
+ return a if a <= max_int else ~(a^mask)
18
19
+ # return sum([a,b])
0 commit comments