Sum of Two integers
两个整数相加不能用加减
用位运算
假设两整数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的更多相关文章
- [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 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 每天一道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 ...
- 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 - ...
- 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 -. ...
- 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 ...
随机推荐
- 基于百度定位及天气获取的DEMO +fragment+sharedpreference
此工程较BaiduLocationXML相比:1.植入fragment,结合微信UI2.在原本主界面的button textview textview 移植到Fragment13.增加网络判断,网 ...
- SharePoint 2013 开发——其他社交功能
博客地址:http://blog.csdn.net/FoxDave 上一篇讲了如何获取用户配置文件的相关属性,它属于SharePoint 2013社交功能的一个小的构成部分.社交功能是SharePoi ...
- C#与XML Schema的问题
http://bbs.csdn.net/topics/50493564 weileily: 用XmlSchema.Read方法读取了一个xsd文件,请问如何遍历检索器中的ComplexType与Sim ...
- 使用response实现文件的下载
package cn.itcast.request; import java.io.FileInputStream;import java.io.IOException;import java.io. ...
- 在Swift中应用Grand Central Dispatch(下)
在第一部分中, 你学到了并发,线程以及GCD的工作原理.通过使用dispatch_barrrier和dispatch_sync,你做到了让 PhotoManager单例在读写照片时是线程安全的.除此之 ...
- 使用POI设置excel背景色
HSSFCellStyle setBorder1 = workbook.createCellStyle(); HSSFFont font1 = workbook.createFont(); font1 ...
- python 的内嵌time模板翻译及说明[转]
一.简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 ...
- 需求文档2_The Battle of Polytopia
需求文档 ------------------------------------- 1. 游戏详细分析 The Battle of Polytopia简要介绍 探索型.策略型的对战塔防游戏,回合制. ...
- Linux(Debian)+Apache+Django 配置
配置Apache和Django连接的过程可谓是一波三折,在此记录. 零.基本的安装 软件环境 l Linux-3.2.0-4-amd64-x86_64-with-debian-7.7 l py ...
- .NET微信自定义分享标题、缩略图、超链接及描述的设置方法
前端Js引用: <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> ...