两个整数相加不能用加减

用位运算

假设两整数a=2和b=6,它们的二进制表示分别为010和110

sum=a^b表示两个二进制数相加不考虑进位:

010

^  110

=  100

carry=(a&b)<<1表示两个二进数相加的进位

010

& 110

= 010

<<1

=100

递归地做sum^carry直到carry=0

代码(一行反而比较好理解):

int getSum(int a, int b) {
return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
}

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

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  3. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

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

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

  6. 【LeetCode】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

    alculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Examp ...

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

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

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

随机推荐

  1. python mysql 更新和插入数据无效

    注意,在删除和增加后必须执行conn.commit()才有效,否则操作无效.

  2. Sharepoint的javascript客户端对象模型获取其他站点的list

    获取当前站点(子站点而不是站点集)下的list var clientContext = new SP.ClientContext.get_current(); var list=clientConte ...

  3. Python的平凡之路(2)

    一.标准库(sys & os):   Python 的标准库(standard library) 是随着 Python 一起安装在你的电脑中的,是 Python 的一部分 (当然也有特殊情况. ...

  4. iOS 开发获取唯一标识

    在做流量精灵的时候有这样一个需求,帐户默认需要取手机的imsi 信息.这就有一个好处,即便用户删除我们的应用后从新下载,下次进入时他们还会以同样的身份登陆,并且获得原先纪录.这样不仅对于开发公司人员来 ...

  5. RF Analyzer for Android 安卓平台连接HackRF的App

    Over the last week I've been working on a new project, trying to build a spectrum analyzer for Andro ...

  6. js:setTimeout 与 setInterval 比较

    在javascript中有两个非常有用的函数:setTimeout 和setInterval ,都是定时器:但是两者存在着一些区别: 1. setTimeout函数 用法:setTimeout(fn, ...

  7. JS中 obj.style.left 与 obj.offsetLeft 的区别

    1.obj.style.left 取得是字符串:28px; 而 obj.offsetLeft取得的值为数字 28: 2.obj.style.left 需要事先将left值写在HTML中,不能致谢在CS ...

  8. Windows2003系统问题:“无法加载安装程序库wbemupgd.dll,或是找不到函数OcEntry.

    “无法加载安装程序库wbemupgd.dll,或是找不到函数OcEntry.请与您的系统管理员联系.特定错误码是 0x7e;" 然后是警告框: " 无法初始化应用程序." ...

  9. [Android自定义控件] Android自定义控件

    转载自:http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理  ...

  10. LeetCode 【47. Permutations II】

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...