不用加减法计算两个整数的和。这道题其实是考察一些基本的布尔代数知识。我们知道,二进制表示时:
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. oracle系列--第三篇 Oracle的安装

    在安装之前,我先说说我的电脑的配置: OS : Windows 7 32bit CPU : 3GHz Memory : 2GB Desk : 320GB ======================= ...

  2. CentoS 下安装gitlab

    curl https://raw.github.com/mattias-ohlsson/gitlab-installer/master/gitlab-install-el6.sh | bash 报错 ...

  3. Update From 用法

    今天遇到用一个表的字段填充另一个表的问题,整理了一下   1.在mysql中,应该使用inner join,即: UPDATE   a INNER JOIN b ON a.userName = b.u ...

  4. SQL SERVER事务处理

    SQL SERVER事务处理   一.事务定义: 事务是单个的工作单元.如果某一事务成功,则在该事务中进行的所有数据更改均会提交,成为数据库中的永久组成部分. 如果事务遇到错误且必须取消或回滚,则所有 ...

  5. 分支语句:if

    (1)分支语句if: if(判断条件) { 满足条件要执行的语句(若满足则alert输出(“”)) } else { 不满足条件时执行的语句 } (若if满足,else绝对不走,反之,走else) 例 ...

  6. Bootstrap - 全局css样式类

    状态类 通过这些状态类可以为行或单元格设置颜色. .active 鼠标悬停在行或单元格上时所设置的颜色 .success 标识成功或积极的动作 .info 标识普通的提示信息或动作 .warning ...

  7. Css3 - 全面学习

    css3实验.生成.学习网站 http://www.css3maker.com/ http://www.css3.me/ 查询前缀和兼容性 http://caniuse.com/ 1.文本阴影 < ...

  8. #在FLAT模式下,需要设置flat子网,VM的IP从这个设置的子网中抓取,这时flat_injected需要设置为True,系统才能自动获得IP,如果flat

    #在FLAT模式下,需要设置flat子网,VM的IP从这个设置的子网中抓取,这时flat_injected需要设置为True,系统才能自动获得IP,如果flat子网和主机网络是同一网络,网络管理员要注 ...

  9. SecureCRT通过console口连接思科设备

     

  10. OpenCV学习笔记——滑动条开关

    由于opencv库中并没有专门为开关而设的函数,可以用滑动条做开关 代码: #include<highgui.h> #include<cv.h> int g_switch_va ...