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

Example 1:

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

Example 2:

  1. Input: a = -2, b = 3
  2. Output: 1
 
  1. class Solution(object):
  2. def getSum(self, a, b):
  3. """
  4. :type a: int
  5. :type b: int
  6. :rtype: int
  7. """
  8. MAX = 0x7FFFFFFF
  9. #符号位为0的最大值
  10. Mask = 0xFFFFFFFF
  11. #符号位为1的最大值
  12.  
  13. while b!=0:
  14. suma=(a^b)&Mask
  15. carry=((a&b)<<1)&Mask
  16. a=suma
  17. b=carry
  18.  
  19. return a if a<=MAX else ~(a^Mask)
  20.  
  21. #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. pip安装报错处理+PyPi源切换教程

    一.pip安装出错类型 1.1 pip版本过旧导致不能安装 报错提示: You are using pip version 9.0.3, however version 10.0.1 is avail ...

  2. weblogic开启http访问日志并实时写入日志文件

    由于http访问会产生大量日志,耗去不少IO和CPU所以在生产一般是不启用的:但有时我们会想启用http访问日志,尤其是在系统上线调试的时候. weblogic的日志默认在domain_name/se ...

  3. MySQL变量变更小记

    MySQL会随版本的更新,在新版本中淘汰一些variable和引入一些新的variable.在配置variable后不起作用或安全扫描取不到variable值产生告警时,可能正是variable变更的 ...

  4. Vue.js 引入外部js方法

    1.外部文件config.js 第一种写法 //常量的定义 const config = { baseurl:'http://172.16.114.5:8088/MGT2' } //函数的定义 fun ...

  5. mysql如何让自增id从某个位置开始设置方法

    一般情况下两种方式: 1.本地数据不需要的情况下直接情况表(尽量不使用

  6. U启动制作U盘启动盘详细教程

    第一步 打开u启动装机版,将准备好的u盘插入电脑usb接口并静待软件对u盘进行识别,由于此次u启动采用全新功能智能模式,可为u盘自动选择兼容性强与适应性高的方式进行制作,相较过去版本可省去多余的选择操 ...

  7. python 类方法中参数使用默认值的方法

    class A(): __init__(self, **arg): self.__dict__.update(arg)        def M(self, config=None, section= ...

  8. Fedora防火墙配置

    简介: Fedora 18以及之后的版本,防火墙的管理不再基于iptables,而基于firewall的东西.firewall的功能相对复杂一些,可以控制服务,控制端口,设置安全区域,设置端口转发等功 ...

  9. js 将文本转换为数据 string number

    <span class="Span" > <p>123.81</p> <a> dejiw</a> </span&g ...

  10. DevExpress v18.1新版亮点——DevExtreme篇(一)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme JavaScript Controls v18.1 的新功能 ...