不用加减法计算两个整数的和。这道题其实是考察一些基本的布尔代数知识。我们知道,二进制表示时:
0 + 0 = 00
1 + 0 = 01
0 + 1 = 01
1 + 1 = 10

所以,两个二进制整数 a 和 b,如果相加的过程中如果没有进位,那么 a+b=a⊗b,这里 ⊗ 表示异或。那么 a+b 的进位为多少呢,只有 1+1 时才会出现进位。所以 a+b 的进位可以表示为 2×(a & b),这里 & 表示两个数字的按位与运算。之所以要乘以 2,是因为要向上进一位。

所以有如下关系:
如果 a,b 时任意的二进制整数。

x0=a⊗bc0=2×(a & b)

那么有

a+b=x0+c0

并且c0 的最低位为 0。
这个过程再进行一遍:

x1=x0⊗c0c1=2×(x0 & c0)

那么有:

a+b=x0+c0=x1+c1

并且 c1 的最后两位都是 0。 这样进行 N 此后,cN 的后 N+1 就都是 0 了。
那么,只要 a,b 是有限位的整数,那么必然可以经过有限次(M次)的这种变换,使得 cM 为 0。这时:

a+b=cM+xM=xM

这个过程很容易写成递归程序:

int getSum(int a, int b)
{
if(a == 0) return b; int x = a ^ b;
int c = (a & b) << 1;
return getSum(c, x);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

当然,也可以用也可以不用递归:

int getSum2(int a, int b)
{
while(a)
{
int x = a ^ b;
a = (a & b) << 1;
b = x;
}
return b;
}

371. Sum of Two Integers的更多相关文章

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

  2. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  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. 371. Sum of Two Integers -- Avota

    问题描述: 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(使用位运算实现)

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

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

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

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

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

  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. BZOJ3837 : [Pa2013]Filary

    当m取2时,k至少为$\frac{n}{2}$ 所以在最优解中每个数被选中的概率至少为$\frac{1}{2}$ 每次随机选取一个位置i,计算出其它数与$a_i$的差值,将差值分解质因数 所有质因数中 ...

  2. BZOJ3808 : Neerc2012 Labyrinth of the Minotaur

    左上角和右下角不四连通等价于左下角和右上角八连通 枚举正方形的左上角,先二分出最大的边长,使得里面不含障碍物 然后再二分出最小的边长,使得两部分连通,用前缀和判断 这题WA了好久…一直对拍都没问题…于 ...

  3. chrome developer tool 调试技巧

    这篇文章是根据目前 chrome 稳定版(19.0.1084.52 m)写的, 因为 google 也在不断完善chrome developer tool, 所以 chrome 版本不同可能稍有差别. ...

  4. POJ 1523 (割点+连通分量)

    题目链接:http://poj.org/problem?id=1523 题目大意:连通图,找图中割点,并计算切除该割点后,图中的连通分量个数. 解题思路: POJ的数据很弱. Tarjan法求割点. ...

  5. CentOS6.4 安装Nagios 并监控端口

    1.下载所需文件nagios-3.4.3.tar.gz,nagios-plugins-1.4.15.tar.gz,nrpe-2.14.tar.gz,sendEmail-v1.56.tar.gz 下载地 ...

  6. Eclipse开发JavaWeb程序报Server Tomcat v7.0 at localhost was unable to start

    出处:http://www.javaweb1024.com/info/582.jspx 原因重现: Eclipse开发JavaWeb程序,启动Servers的Tomcat服务器,突然跳出弹出框,内容显 ...

  7. java获取获得Timestamp类型的当前系统时间

    java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...

  8. CentOS 下安装无线哥的老爷机DELL的无线驱动

    使用命令检测网卡 lspci | grep Network   为“0c:00.0 Network controller: Broadcom Corporation BCM4312 802.11b/g ...

  9. JS中的闭包的一些理解!

    在日常的开发中,基本上是不会遇到关于闭包的这样,但是,因为在一些特殊的情况下,必须采用闭包,所以这里简单的概述下什么是闭包: OK!简而言之,闭包只是一个名词而已,我们更注重于他所实现的功能,也就是我 ...

  10. Ubuntu 循环遍历当前目录下所有文本文件中的字符

    sudo grep -n 'xxxx' -r ./*