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.


题目标签:Bit Manipulation

  这道题目让我们做两数之和,当然包括负数,而且不能用+,-等符号。所以明显是让我们从计算机的原理出发,运用OR,AND,XOR等运算法则。一开始自己想的如果两个数都是正数,那么很简单,运用XOR  ^ 去找出所有的单一的1。接着运用AND &去找出所有重复的1;重复的1就相当于carryover,需要进位。然后运动<<把重复的1给进位就可以了,最后直接OR一下就等于答案(这是错的,需要每次循环来判断新的进位)。但是发现这个是能运用于两个正数,研究来研究去,不会算带负数的,所以放弃网上找答案。

  发现答案不和我的两个正数之和算法一样嘛!唯一不同的就是答案是把算出的答案继续带回function直到carry等于0;

通过例子来看一下:

a = 5, b = 1:

a: 101

b: 001

根据我最初的算法:(错误的)

sum = a ^ b = 100

carry = a & b = 001 这里这个1就是需要进位的

carry = 001 << 1 = 010

最后把sum 100 和 carry 010 OR 一下 就等于 110 = 6。

但是答案的做法却是把sum 和 carry在带回function继续算直至carry = 0, 我们来看一下例子:

a = 5, b = 1:

a = 101

b = 001

sum = 100

carry = 010

带回

a = 100

b = 010

sum = 110

carry = 000 这里等于0了,所以结束,我的理解是,答案的做法是把carryover带回去,和sum比较,如果这一次没有继续需要进位的数字了,就可以结束,否则继续下一轮;换一句话就是,答案是把每一轮的sum和carryover拿出来,下一轮继续加一起看一看有没有新的需要进位的地方,所以明显我之前的做法是错的,我只考虑了一轮而已,实际上是每一轮都有可能有新的需要进位的地方。

那新的问题又来了,为啥负数也可以,这里的负数是2‘s complement:

比如说 -5 = 1111 1111 1111 1111 1111 1111 1111 1011

为何-5 是这样: 首先把上面的bits -1

      1111 1111 1111 1111 1111 1111 1111 1010

然后再flip一下

        0000 0000 0000 0000 0000 0000 0000 0101 = 5. 所以负数都需要先flip一下,然后+1 便成了上面那样。

带负数的两数之和,有点麻烦就是有那么多1,所以利用了自己的想象力来帮助自己理解:(不知道对不对)

举个例子:

a = -5, b = 15

把每一个sum (a) 和 carry (b) 打出来是这样的:

11111111111111111111111111111011
1111
11111111111111111111111111110100
10110
11111111111111111111111111100010
101000
11111111111111111111111111001010
1000000
11111111111111111111111110001010
10000000
11111111111111111111111100001010
100000000
11111111111111111111111000001010
1000000000
11111111111111111111110000001010
10000000000
11111111111111111111100000001010
100000000000
11111111111111111111000000001010
1000000000000
11111111111111111110000000001010
10000000000000
11111111111111111100000000001010
100000000000000
11111111111111111000000000001010
1000000000000000
11111111111111110000000000001010
10000000000000000
11111111111111100000000000001010
100000000000000000
11111111111111000000000000001010
1000000000000000000
11111111111110000000000000001010
10000000000000000000
11111111111100000000000000001010
100000000000000000000
11111111111000000000000000001010
1000000000000000000000
11111111110000000000000000001010
10000000000000000000000
11111111100000000000000000001010
100000000000000000000000
11111111000000000000000000001010
1000000000000000000000000
11111110000000000000000000001010
10000000000000000000000000
11111100000000000000000000001010
100000000000000000000000000
11111000000000000000000000001010
1000000000000000000000000000
11110000000000000000000000001010
10000000000000000000000000000
11100000000000000000000000001010
100000000000000000000000000000
11000000000000000000000000001010
1000000000000000000000000000000
10000000000000000000000000001010
10000000000000000000000000000000
1010
0
10

我们可以看到最后是10,在我理解,有负数的情况下,我们需要把负数的那些1都过滤一下,所以循环的次数会多很多,相对于正数来说。

通过上面规律,每次a都是减少它的1的数量,每次b都是增多它的0的数量,直到a的1过滤完,b的0达到极限,便结束了,得到正确答案。

利用想象力的时候到了,这就相当于a= -5 b= 15 在一个横坐标上,每一次a向右走一格,b像左走一格,或者说是负数向右走,正数向左走,直到一个最小的负数走到0,那么另外一个数就是答案。

Java Solution:

Runtime beats 9.26%

完成日期:06/27/2017

关键词:Bit Manipulation

关键点:利用xor ^ 拿到所有的单一的1;利用and &拿到所有重复的1,就是需要进位的1,利用 << 把1像左进位

 public class Solution
{
public int getSum(int a, int b)
{
if(b==0)
return a; int sum = a ^ b;
int carry = (a & b) << 1; return getSum(sum, carry);
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/5631814.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 371. Sum of Two Integers (两数之和)的更多相关文章

  1. [LeetCode] 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. 371 Sum of Two Integers 两整数之和

    不使用运算符 + 和-,计算两整数a .b之和.示例:若 a = 1 ,b = 2,返回 3. 详见:https://leetcode.com/problems/sum-of-two-integers ...

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

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

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

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

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

  7. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  8. leetcode实践:通过链表存储两数之和

    题目: 两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的 ...

  9. LeetCode 633. Sum of Square Numbers平方数之和 (C++)

    题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b suc ...

随机推荐

  1. 子元素设定margin值会影响父元素

    有些情况下,我们设定父元素下的子元素margin值时,父元素会被影响. 这是个常见问题,而且只在标准浏览器下 (FirfFox.Chrome.Opera.Sarfi)产生问题,IE下反而表现良好. 例 ...

  2. linux 环境NTP配置与开机自启动(转)

    Linux下配置NTP服务器一.前言:    默认NTP服务端口:    UDP/123    本文配置的NTP工作模式:    使用client/server方式,该方式适用于一台时间服务器接收上层 ...

  3. 上传文件复用代码【fileUpload】

    这是使用了FileUpload上传组件的,解决了中文乱码问题了,并且删除了临时文件的. 使用了一个Book对象做示范 private Book uploadData(HttpServletReques ...

  4. HashMap、HashTable、ArrayList、LinkedList、Vector区别

    HashTable和HashMap区别 ①继承不同. public class Hashtable extends Dictionary implements Map public class Has ...

  5. 手机管家iPhoneX的适配总结

    WeTest 导读 随着苹果发布会的结束,Xcode的GM版也上线了,也意味着iPhoneX适配之旅的开始. 一.设计关注篇 注意设计的基本原则:(苹果呼吁的) 规格原帖:https://develo ...

  6. 匹配替换第n个字符串

    var name = "questions[0][question]",i=0; name.replace(/\[.+?\]/g, function(match, pos, ori ...

  7. Codeforce E. Fire

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  8. JDFS:一款分布式文件管理系统,第三篇(流式云存储)

    一 前言 看了一下,距离上一篇博客的发表已经过去了4个月,时间过得好快啊.本篇博客是JDFS系列的第三篇博客,JDFS的目的是为了实现一个分布式的文件管理系统,前两篇实现了基本的上传.下载功能,但是那 ...

  9. Poj 1032 Parliament

    Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19103   Accepted: 8101 Descr ...

  10. NOIP2017SummerTraining0717

    个人感受:自己水平是真的差劲,和他们不是一个档次的,第二题,如果不是陈载元暴力过了,我也不会那么早去A了第二题,第一题真的是无语,以前做到过,还想到了每个对应值a[i]-i,但是没想出来,真的是 可惜 ...