作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/sum-of-two-integers/description/

题目描述

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

Example:
Given a = 1 and b = 2, return 3.

题目大意

不使用±号,完成两个数字的加法。

解题方法

位运算

在不准使用+和-的情况下实现两个整数的加法,那么肯定要用到位运算了。我们考虑位运算加法的四种情况:

0 + 0 = 0

1 + 0 = 1

0 + 1 = 1

1 + 1 = 0(with carry)

XOR的一个重要特性是不进位加法,那么只要再找到进位,将其和XOR的结果加起来,就是最后的答案。通过观察上面的四种情况我们可以发现,只有在两个加数的值都是1的时候才会产生进位,所以我们采用&来计算进位的情况,但是注意到由于是进位,所以我们必须要将&的结果左移一位,然后再和XOR的结果相加。

举例来看:

再举个例子: 11+5

其二进制形式为11: 1011, 5: 0101

1. 那么两个位置都为1的地方就需要进位, 所以进位值就为0001. 原位置两个数相加的结果为那个位置值的异或即1110, 即两个位置值如果不一样就为1, 一样的话要么两个位置原来值都为0结果也为0, 要么进位, 那么结果依然是0. 

2. 接下来就要把进位位和下一位相加, 所以进位值左移一位,即0001变为0010, 重复上面操作可得新的**进位值**为0010, 原位置异或(即相加)结果为1100.

3. 继续重复上面操作直到进位为0, 可得到最终结果10000, 即16

这个题的做法就是用a保存“直接加”(不考虑进位)的结果,用b保存进位;然后使a再与b相加,直至保存进位的b为0.

“直接加”通过XOR实现,进位通过and实现。

所以困扰了我两年的题终于想明白了。。

class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
# 32 bits integer max
MAX = 0x7FFFFFFF
# 32 bits interger min
MIN = 0x80000000
# mask to get last 32 bits
mask = 0xFFFFFFFF
while b != 0:
# ^ get different bits and & gets double 1s, << moves carry
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
# if a is negative, get a's 32 bits complement positive first
# then get 32-bit positive's Python complement negative
return a if a <= MAX else ~(a ^ mask)

日期

2018 年 2 月 26 日
2018 年 11 月 11 日 —— 剁手节快乐

参考:
http://blog.csdn.net/qq508618087/article/details/51789576
https://www.cnblogs.com/dyzhao-blog/p/5662891.html

【LeetCode】371. Sum of Two Integers 解题报告(Python)的更多相关文章

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

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

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

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

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

  5. LeetCode: 371 Sum of Two Integers(easy)

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

  6. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

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

  9. 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) { ...

随机推荐

  1. abandon, abbreviation

    abandon 近/反义词: continue, depart, desert (做动词时读作diˈzəːt), discard, give up, quit, surrender搭配: altoge ...

  2. EDA简介

    Electronic design automation (EDA), also referred to as electronic computer-aided design (ECAD),[1] ...

  3. Angular 组件通信的三种方式

    我们可以通过以下三种方式来实现: 传递一个组件的引用给另一个组件 通过子组件发送EventEmitter和父组件通信 通过serive通信 1. 传递一个组件的引用给另一个组件 Demo1 模板引用变 ...

  4. webservice--常用注解

    定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...

  5. JAVA中的六种日期类型使用

    基本的6种日期类 /** * 六种时间类型的类 * 数据库格式的时间三种格式 */ java.util.Date date = new java.util.Date();//年与日时分秒 //数据库的 ...

  6. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  7. docker安装jumpserver

    注意MySQL的密码设置要有复杂度,否则jumpserver用不了 #先准备一台服务器安装MySQL和redis(注意官网版本要求) root@ubuntu:~# docker pull mysql: ...

  8. react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.

    错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...

  9. Spring MVC与html页面的交互(以传递json数据为例)

    一.导入相jar包 主要包括spring相关jar包和fastjson jar包,具体步骤略. 二.配置相关文件 1.配置web.xml文件 <?xml version="1.0&qu ...

  10. 【VSCode】检测到 #include 错误。请更新 includePath。已为此翻译单元(C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\i686-

    win+r 运行cmd 输入"gcc -v -E -x c -"获取mingw路径: 我的: #include "..." search starts here ...