mycode:

没思路啊。。。二级制四则运算不熟悉。。。

参考:

既然不能使用加法和减法,那么就用位操作。下面以计算5+4的例子说明如何用位操作实现加法:
1. 用二进制表示两个加数,a=5=0101,b=4=0100;
2. 用and(&)操作得到所有位上的进位carry=0100;
3. 用xor(^)操作找到a和b不同的位,赋值给a,a=0001;
4. 将进位carry左移一位,赋值给b,b=1000;
5. 循环直到进位carry为0,此时得到a=1001,即最后的sum。

上面思路还算正常,然而对于Python就有点麻烦了。因为Python的整数不是固定的32位,所以需要做一些特殊的处理,具体见代码吧。
代码里的将一个数对0x100000000取模(注意:Python的取模运算结果恒为非负数),是希望该数的二进制表示从第32位开始到更高的位都同是0(最低位是第0位),以在0-31位上模拟一个32位的int。

注意:

对于:该数的二进制表示从第32位开始到更高的位都同是0  ~  方法一:取余数 方法二:异或

return:不懂。。?????

class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
while b != 0:
carry = a & b
a = (a ^ b) % 0x100000000
b = (carry << 1) % 0x100000000
return a if a <= 0x7FFFFFFF else a | (~0x100000000+1)
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
MAX = 0x7FFFFFFF
MIN = 0x80000000
mask = 0xFFFFFFFF
while b != 0:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask return a if a <= MAX else ~(a ^ mask)

leetcode-mid-math-371. Sum of Two Integers-NO-???的更多相关文章

  1. [LeetCode&Python] Problem 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 ...

  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 (两数之和)

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

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

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

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

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

  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 -. ...

  10. 【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. qtdebug和release加载不同的文件配置

    win32:CONFIG(release, debug|release): { LIBS +=$$PWD/../../../thirdparty\qwt\lib\qwt.lib LIBS +=$$PW ...

  2. js自执行函数

    5.1对于函数表达式,在后面加括号即可以让函数立即执行:例如下面这个函数,至于为什么加了括号就可以立即执行,我们可以这么理解,就是像fn1():这样写的话,函数 可以立即执行是没问题的,我们在经常会用 ...

  3. MySQL 主从同步架构中你不知道的“坑”

    以下操作征对指定不同步库 binlog-format=ROW模式 1 查看主从的binlog模式 mysql> show slave status\G ********************* ...

  4. django admin站点美化

    使用django-simpleui 安装 pip install  django-simpleui 源码地址 https://github.com/newpanjing/simpleui django ...

  5. vue video.js使用

    插件github地址:https://github.com/videojs/video.js 这边做一个切换视频vue url的解决记录 this.$nextTick(() => { let u ...

  6. C语言面试相关知识点

    1.关键字static的作用是什么? 有三个明显的作用: 1)在函数体内,一个被声明为静态的变量在这个函数被调用过程中维持其值不变 2)在模块内(但在函数体外),静态的变量可以被模块内所有函数访问,但 ...

  7. Redis的频道发布与消息订阅

    Redis的频道发布与消息订阅 官网介绍 进程间的一种通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 订阅/发布消息图 下图展示了频道channel1,以及订阅这个频道的三个客户端-c ...

  8. 第五小节之JAVA IO流

    文件:文本文件是一种计算机文件,它是一种典型的顺序文件,其文件的逻辑结构又属于流式文件,特别的是指以ASCLL码方式(也称为文本方式)存储的文件,而更确切地说,英文.数字等字符存储的是ASCLL码,而 ...

  9. 修改docker自定义网桥后的默认网卡名称

    [root@docker2 ~]# docker  network   create  --subnet=10.10.10.0/24  docker1   #新键网桥docker142323044a4 ...

  10. RedHat7安装yum工具

    一                   RedHat7安装yum工具 1.1        查看yum是否可用 yum list yum repolist 1.2        卸载原yum rpm ...