LeetCode 371. Sum of Two Integers
2016-09-20 23:35
417 查看
Calculate the sum of two integers a and b, but you are not allowed to use the operator
Example:
Given a = 1 and b = 2, return 3.
题目要求:计算两个整型的和,但是不能用+和-
我们知道a+b为((a&b)<<1)+(a^b),因此可以用递归的方法
+and
-.
Example:
Given a = 1 and b = 2, return 3.
题目要求:计算两个整型的和,但是不能用+和-
我们知道a+b为((a&b)<<1)+(a^b),因此可以用递归的方法
class Solution { public: int getSum(int a, int b) { if (a == 0)return b; if (b == 0)return a; return getSum((a&b)<<1,a^b); } };
相关文章推荐
- 堆和栈 各种变量存储位置
- ReactiveCocoa入门教程:第二部分
- HDU 3018 Ant Trip 一笔画问题
- Xml帮助类
- 【转】十个JavaScript中易犯的小错误,你中了几枪?
- 迷惘。。。。。。。
- 一道面试题的解答_骑士获得金币问题
- 手势操作
- 01背包
- SSH如何通过公钥连接云服务器
- SpringMVC学习指南-前言
- 揭露QPS增高后的秘密
- Xcode下的DerivedData文件
- 性能测试
- Could not locate device support files.《This iPhone 5 (Model A1429) is running iOS 7.0.4 (11B554a), which may not be supported by this version of Xcode.》-b
- Android NDK学习笔记11-JNI异常处理
- 复习
- source insight养眼主题monokia
- 我学Hibernate - 03 实体类常用注解
- unity3d--摄像机