博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode&Python] Problem 371. Sum of Two Integers
阅读量:4556 次
发布时间:2019-06-08

本文共 652 字,大约阅读时间需要 2 分钟。

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2Output: 3

Example 2:

Input: a = -2, b = 3Output: 1
 
class Solution(object):    def getSum(self, a, b):        """        :type a: int        :type b: int        :rtype: int        """        MAX = 0x7FFFFFFF        #符号位为0的最大值        Mask = 0xFFFFFFFF        #符号位为1的最大值                    while b!=0:            suma=(a^b)&Mask            carry=((a&b)<<1)&Mask            a=suma            b=carry                return a if a<=MAX else ~(a^Mask)#Python 比较麻烦的地方是 要Mask.....

  

转载于:https://www.cnblogs.com/chiyeung/p/9928153.html

你可能感兴趣的文章
生成随机验证码
查看>>
font-family,font-size,color
查看>>
平安夜和圣诞节
查看>>
Search Insert Position
查看>>
数据可视化(5)--jqplot经典实例
查看>>
u盘复制提示文件过大
查看>>
grails项目数据源配置
查看>>
mysql数据库索引简单原理
查看>>
【爱笑话7.0版】笑话两万篇,免费阅读,绝无广告
查看>>
The square chest
查看>>
不用第三个变量实现a,b的值交换
查看>>
四则运算
查看>>
为VS2010默认模板添加版权信息(转)
查看>>
int类型属性判空
查看>>
remote: ERROR: missing Change-Id in commit message footer
查看>>
js中的事件总结
查看>>
关于Unity实现三维物体裁剪功能
查看>>
BZOJ4033 [HAOI2015]树上染色 【树形dp】
查看>>
POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)
查看>>
最少构造出回文 (最长公共子序列+思维)
查看>>