不用加减法计算两个整数的和。这道题其实是考察一些基本的布尔代数知识。我们知道,二进制表示时:
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. ios 适应屏幕

    标准做法:auto layout 其中:也可以用代码判断  1.如果不是hardcode的话,又怎么会不兼容呢..  2.如果你全写320*480也是兼容吗 3.写320嘛的就是hardcode啊.  ...

  2. 【wikioi】1004 四子连棋

    题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...

  3. MySQL 用户管理——权限表

    权限表 权限表存放在mysql数据库中 user表结构 用户列:Host.User.Password 权限列:*priv 资源控制列:max* 安全列:其余   db表 存储了用户对某个数据库的操作权 ...

  4. grep 命令搜索 带空格的字符

    grep - n ' a[[:space:]]b' 就能搜索到 'a    b'类似的字符了 如果要搜索带单引号的 用双引号括起来 如果要搜索带双引号的 用单引号括起来

  5. Silic PHP大马Cookie欺骗漏洞

    $password="ebd9a3c106064a255aaee28b6eb4f21c"; if($_COOKIE['admin_silicpass'] != md5($passw ...

  6. jQuery.qrcode.js客户端生成二维码,支持中文并且可以生成LOGO

    描述: jquery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jquery插件 ,使用它可以很方便的在页面上生成二维条码.此插件是能够独立使用的,体积也比较         ...

  7. [转]如何:定义和处理 SOAP 标头

    本文转自:http://msdn.microsoft.com/zh-cn/library/vstudio/8728chd5(v=vs.100).aspx 本主题专门介绍一项旧有技术.现在应通过使用以下 ...

  8. FastDFS 安装

    FastDFS(centerOs) 安装包:FastDFS_v5.07.tar libfastcommon-master.zip(是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库 ...

  9. windows2003下安装apache

    php建议在http://www.apachelounge.com/ 下载apache的二进制版本,因为提供了不同VC版本.不懂,照做. 因为是在虚拟机2003上面搞的,需要装VC2010版本. 下载 ...

  10. 嵌入式 如何定位死循环或高CPU使用率(linux) 及性能优化

    嵌入式 如何定位死循环或高CPU使用率(linux) ln -s /mnt/nfs/_install/usr/bin/sort /usr/bin/sort awk '{print $1,$2,$14, ...