1. 程式人生 > >【leetcode 簡單】 第八十七題 兩整數之和

【leetcode 簡單】 第八十七題 兩整數之和

turn div 計算 pre lee nbsp gets tco shu

不使用運算符 +-,計算兩整數ab之和。

示例:
a = 1 ,b = 2,返回 3。

class Solution:
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
      #  return sum([a,b])
        first=a^b
        second=(a&b)<<1
        return sum([first,second])

參考:https://www.jianshu.com/p/3bdba23a0401

【leetcode 簡單】 第八十七題 兩整數之和