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 ...
随机推荐
- Python开发入门与实战4-模板页面
4.Django基于模板页面 在前一章中,HTML是直接被硬编码在 Python views.py代码中,如下: from django.http import HttpResponse import ...
- Android Sudoku应用挂掉的问题
在真机上测试数独游戏时发现,快速点击屏幕时,游戏偶尔出现挂死的情况,Log如下 04-08 15:35:00.838 7317-7356/org.elvalad.sudoku D/OpenGLRend ...
- How to implement updatable view with NHibernate
see : http://msdn.microsoft.com/en-us/library/ms187956.aspx(The constrains of creation updatable vie ...
- 从零开始学习Node.js例子三 图片上传和显示
index.js var server = require("./server"); var router = require("./router"); var ...
- 转:GROUPING SETS、ROLLUP、CUBE
转:http://blog.csdn.net/shangboerds/article/details/5193211 大家对GROUP BY应该比较熟悉,如果你感觉自己并不完全理解GROUP BY,那 ...
- [翻译]利用REDIS来搭建可靠分布式锁的提议
本系列都是翻译REDIS作者的博文 另外加上我自己的一点点理解 希望有问题大家一起讨论 http://antirez.com/news/77 原文地址 在利用REDIS做分布式锁时基本持有2种观点 ...
- Git 使用juju
写在前面: 想不到好标题,就写好文章吧. 此篇主要是介绍在使用git过程中常见的一些命令以及遇到的问题,手册+答疑解惑! 在git未玩得通透熟练之际,此篇文章的标题序号无任何意义. 1.git 版本回 ...
- hdu4737 A Bit Fun
link:http://acm.hdu.edu.cn/showproblem.php?pid=4737 暴力可以过啊.O(N^2)的复杂度. #include <cstdio> ]; in ...
- [NOIP2009] 靶形数独(搜索+剪枝)
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...
- 在笔记本电脑开通无线WIFI
1.Windows + R启动运行,输入services.msc进入服务 2.在服务中将Security Center服务从自动启动转为禁止启动 3.在服务中将Windows Firewall的启动类 ...