作者: 负雪明烛
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. php header下载文件 无法查看原因

    php header下载文件 无法查看原因 php header下载文件 下方函数可以下载单个文件 function download($file_url){ if(!isset($file_url) ...

  2. 文件和目录之间建立链接 (ln)

  3. Scala【需求二:求各省市的各个指标】

    需求处理步骤 原始数据->json->过滤->列裁剪 需求二:求各省市的各个指标 原始数据 文本pmt.json,每一行都是一个json字符串.里面包含ip等信息 {"se ...

  4. 数据存储SharePreferences详解

    1.SharedPreferences存储 SharedPreferences时使用键值对的方式来存储数据的,也就是在保存一条数据时,需要给这条数据提供一个对应的键,这样在读取的时候就可以通过这个键把 ...

  5. LINUX 系统性能检测工具vmstat

    vmstat 有2个参数,第一个是采样时间间隔(单位是s),第二个参数是采样个数. #表示 2s采样一次,一共采样2次 vmstat 2 2 也可以只写第一个参数,让系统一直采样直到停止(ctrl + ...

  6. java中super的几种用法,与this的区别

    1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base"); ...

  7. OC-私有方法,构造方法,类的本质及启动过程

    总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...

  8. java中的原子操作类AtomicInteger及其实现原理

    /** * 一,AtomicInteger 是如何实现原子操作的呢? * * 我们先来看一下getAndIncrement的源代码: * public final int getAndIncremen ...

  9. Spring(2):依赖注入DI

    依赖注入DI 当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例.但在Spring里,创建被 ...

  10. 连接 MySQL 数据库出现问题:The server time zone value ‘�й���׼ʱ��‘ is unrecogni....

    出现问题 The server time zone value '�й���׼ʱ��' is unrecogni.... 解决方案 在 URL 后面加上 ?serverTimezone=UTC 如下: ...