leetcode-371. Sum of Two Integers
2016-07-17 00:44
471 查看
1、来源:点击打开链接
2、题目:
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.
3、代码(主要还是参考了这里点击打开链接):
class Solution {
public:
int getSum(int a, int b) {
int c,d;
c=a^b;
d=a&b;
return d==0?c:(getSum(c,(d<<1)));
}
};
4、原理1:
原理2:
如果a,b相加没有进位,则a^b即为a+b;
若是有进位,则可以使用a^b,与(a&b)<<1相加即可
5、加油啦~~写一点点博客也好,也要坚持下呢,小hao同学加油~
2、题目:
Calculate the sum of two integers a and b, but you are not allowed to use the operator
+and
-.
Example:
Given a = 1 and b = 2, return 3.
3、代码(主要还是参考了这里点击打开链接):
class Solution {
public:
int getSum(int a, int b) {
int c,d;
c=a^b;
d=a&b;
return d==0?c:(getSum(c,(d<<1)));
}
};
4、原理1:
0 | 1 | |
0 | 00 | 01 |
1 | 01 | 11 |
如果a,b相加没有进位,则a^b即为a+b;
若是有进位,则可以使用a^b,与(a&b)<<1相加即可
5、加油啦~~写一点点博客也好,也要坚持下呢,小hao同学加油~
相关文章推荐
- linux C 打印错误信息和标准输入输出
- java中基础的Annotation--@Override @Deprecated @SuppressWarnings
- SpringBoot Quickstart
- 模拟乘法运算之N的阶乘(大数阶乘)
- Linux 新建用户、用户组,给用户分配权限(chown、useradd、groupadd、userdel、usermod、passwd、groupdel)
- 致努力的自己
- SpringBoot Quickstart
- 配置python tab键自动补全
- HTML基础课程笔记
- 构造方法探析
- hibernate中criteria的用法
- java与xml之DOM创建和解析XML文档
- 折半查找(二分法)
- Ubuntu16.04 安装openjdk-7-jdk
- LeetCode--No.144--Binary Tree Preorder Traversal
- Android要学的
- ffmpeg AVRational结构体介绍
- [转]ListView学习笔记(二)——ViewHolder
- 使用游标更新和删除数据
- 常回家看看