不用加减法计算两个整数的和。这道题其实是考察一些基本的布尔代数知识。我们知道,二进制表示时:
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. BZOJ3578 : GTY的人类基因组计划2

    关于如何判断一个集合是否出现过: 给每个元素随机一个hash权值,然后xor起来即可 插入删除都只需xor 线段树维护区间有效人数和,以及打标记表示这个区间的集合要全部标记为出现过,并把区间内sum值 ...

  2. splice JavaScript Array 对象

    定义和用法 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 语法 arrayObject.splice(index,howmany,item1, ...

  3. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  4. 洛谷 P1008 三连击 Label:水

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行,每行3个 ...

  5. TYVJ P1088 treat Label:鞭笞人的DP

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 广东汕头聿怀初中 Train#2 Problem2 描述 给出长度为N的数列{A_i},每次可以从最左边或者最 ...

  6. 【wikioi】1004 四子连棋

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

  7. RN组件之ToolbarAndroid

    一.ToolbarAndroid 1.该组件封装了Android平台中的ToolBar组件(只适用于Android平台).一个ToolBar组件可以显示一个Logo图标 以及一些导航图片(例如:菜单功 ...

  8. jquery-mobile表单提交问题

    关于使用jquery-mobile表单提交遇到的问题     当你使用了jquery-mobile的时候,如果你在前台提交一个了一个form表单,而在后台你处理完业务逻辑之后想要重定向到另一个方法或页 ...

  9. 使用Objective-C的文档生成工具:appledoc

    使用Objective-C的文档生成工具:appledoc 前言 做项目的人多了,就需要文档了.今天开始尝试写一些项目文档.但是就源代码来说,文档最好和源码在一起,这样更新起来更加方便和顺手.象 Ja ...

  10. Stack and Heap 堆和栈的区别

    在和计算机内存打交道时,我们一定会碰到堆和栈,这两个东西很容易搞混,那么现在就来梳理一下二者的关系. 栈是用来静态分配内存的而堆是动态分配内存的,它们都是存在于计算机内存之中. 栈的分配是在程序编译的 ...