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 = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 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.....

  

[LeetCode&Python] Problem 371. Sum of Two Integers的更多相关文章

  1. [LeetCode&Python] Problem 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  2. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  3. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  4. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  5. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  6. LeetCode 371. Sum of Two Integers (两数之和)

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

  7. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  8. LeetCode 371. Sum of Two Integers

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

  9. Leetcode 371: Sum of Two Integers(使用位运算实现)

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

随机推荐

  1. servlet/和/*匹配的区别

    两者真正的区别是,两者的长度不同,根据最长路径匹配的优先级,/*比/更容易被选中,而/的真正含义是,缺省匹配.既所有的URL都无法被选中的时候,就一定会选中/,可见它的优先级是最低的,这就两者的区别.

  2. 使用Spring-data-jpa(2)(三十一)

    创建实体 创建一个User实体,包含id(主键).name(姓名).age(年龄)属性,通过ORM框架其会被映射到数据库表中,由于配置了hibernate.hbm2ddl.auto,在应用启动的时候框 ...

  3. oracle如何创建表的自增ID(通过触发器)

    Oracle中创建表的自增ID(通过触发器),序列的自增ID和触发器的自增ID的区别 1.新增数据(序列) --创建示例表 -- create table Student( stuId ) not n ...

  4. 转 举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤

    MATLAB Coder可以从MATLAB代码生成独立的.可读性强.可移植的C/C++代码. http://www.mathworks.cn/products/matlab-coder/ 使用MATL ...

  5. Win10系列:UWP界面布局基础6

    资源合并 前面提到过,可以将资源字典定义在单独的XAML文件中,这样的文件被称为资源字典文件.那么,在需要引用文件中的资源时可以通过ResourceDictionary元素的MergedDiction ...

  6. day21 MRO和C3算法

    核能来袭 --MRO和C3算法 1. python的多继承 2.python经典类的MRO 3.python新式类的MRO, C3算法 4.super 是什么鬼? 一.python的多继承 在前面的学 ...

  7. 根据题目完成以下50道SQL语句

    已知有如下4张表: 学生表:STUDENT(S#,SNAME,SAGE,SSEX) 课程表:COURSE(C#,CNAME,T#) 成绩表:SC(S#,C#,SCORE) 教师表:TEACHER(T# ...

  8. Android : 输入设备键值从底层到应用层的映射流程

    一.Android输入子系统简介: Android输入事件的源头是位于/dev/input/下的设备节点,而输入系统的终点是由WMS管理的某个窗口.最初的输入事件为内核生成的原始事件,而最终交付给窗口 ...

  9. AngularJS2.0教程(一)快速上手之基础知识

    Why Angular2 Angular1.x显然非常成功,那么,为什么要剧烈地转向Angular2? 性能的限制 AngularJS当初是提供给设计人员用来快速构建HTML表单的一个内部工具.随着时 ...

  10. Session和Cookie,以及用户登录验证问题。

    参考 :https://blog.csdn.net/u010002184/article/details/79416199 https://www.bbsmax.com/A/RnJW72YJqY/ 首 ...