两个整数相加不能用加减

用位运算

假设两整数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. GPS模块启动模式说明

    1.启动方式 GPS开机定位分为冷启动、温启动和热启动三种:  1、冷启动:以下几种情况开机均属冷启动。初次使用时;电池耗尽导致星历信息丢失时;关机状态下将接收机移动1000公里以上距离。   2、温 ...

  2. uoot启动过程

    1.从我们的start_armboot开始讲起 u-boot整体由汇编段和C语言段外加连接脚本组成.关于汇编段请看我之前的博客<u-boot源码汇编段简要分析>,好,让我们进入start_ ...

  3. 使用PHPUnit + Selenium进行自动化测试

    project: blog target: how-to-use-phpunit-selenium-test.md date: 2015-12-22 status: publish tags: - S ...

  4. Libgdx 开发指南(1.3) 应用框架——查询、日志

    查询 Application接口提过多种方法查询运行时环境属性. 获得应用类型 有时候根据运行平台需要处理一些具体的逻辑,可以使用 Application.getType() 方法来返回应用所运行的平 ...

  5. Mark一下,一上午就这么过去了,关于客户端连接oracle10G的问题

    Mark一下,一上午就这么过去了,关于客户端连接oracle10G的问题 正常的客户端PLSQL和Navicat都可以正常连接Oracle(局域网内),但代码生成器和VS2015死活连不上,在网上找了 ...

  6. Linux平台屏幕录像工具RecordMyDesktop

    如果你把Linux桌面效果自定义得很漂亮,是不是很想录下来和其他人一起分享呢?RecordMyDesktop完全满足需要,我期待这么一个软件很久了,以前也一直没有找到合适的. 以后每次发Ubuntu的 ...

  7. iar 错误解决

    使用原来备份的项目可以正确烧写并进入调试状态,但使用新项目则报错,错误提示为Failed to load debugee: E:\工作\项目-农业\KaCES-F\Debug\Exe\kaces.tx ...

  8. 腾讯优测干货精选|Android双卡双待适配——隐藏在数据库中的那些秘密

    腾讯优测是专业的app自动化测试平台,除了提供兼容性测试,远程真机租用等多维度的测试服务,还有优分享-腾讯内部的移动研发测试干货精选~ 许多APP都希望获取用户通讯录联系人,利用通讯录关系链信息来丰富 ...

  9. request.getParameterMap()使用方法

    我习惯于加密完 重定向 : Map<String,String[]> getMap = request.getParameterMap(); String[] a = getMap.get ...

  10. C语言实现的Web服务器(转-kungstriving)

    自己研究了好几天终于写出来一个,哈哈,当然也从网上得到了很多的帮助拉.谢谢大家咯!这个版本还不是很完善,但Web服务器的基本框架已经出来了,还有部分的功能需要进行进一步的测试和修改.虽然说C的开发比较 ...