不使用运算符 +
和 -
,计算两整数 a
、b
之和。
示例 1:
输入: a = 1, b = 2 输出: 3
示例 2:
输入: a = -2, b = 3 输出: 1
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
temp1=(a&b)<<1 #需要进位的
temp2=a^b #不需要进位的
return sum([temp1,temp2])
f=Solution()
print(f.getSum(2,3))