leetcode 371
来源:互联网 发布:多线程并发 数据共享 编辑:程序博客网 时间:2024/06/17 15:22
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.
Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
Subscribe to see which companies asked this question.
Show Similar Problems
不用加减实现加法
思路:使用位运算 貌似负数位运算没有什么需要特别考虑的
a+b
若a, b 某一位都为1,那么进位
所以c = a&b<<1
剩下的即为一个1一个0
d = a ^ b
a + b = c + d
进行迭代:
代码:
class Solution {
public:
int getSum(int a, int b) {
if(a == 0)
return b;
if(b == 0)
return a;
int c = a & b;
int d = a ^ b;
c<<=1;
return getSum(d,c);
}
};
public:
int getSum(int a, int b) {
if(a == 0)
return b;
if(b == 0)
return a;
int c = a & b;
int d = a ^ b;
c<<=1;
return getSum(d,c);
}
};
0 0
- leetcode 371
- leetcode 371
- leetcode 371 python
- Leetcode 371(Java)
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- iOS 蓝牙开发 app作为外设被连接的实现(三)
- 百度音乐api接口
- 关于事件分发的一些认识
- BootStrap3.X模态框垂直居中显示
- MQTT V3.1--我的理解
- leetcode 371
- NBUT1597:Find MaxXorSum(字典树)
- Python引用(import)文件夹下的py文件的方法
- Android 静态代码分析
- 南宁H5EDU学员html/css阶段项目
- SSL 2628——2012年佛山市GDOI选拔赛题 循环小数【数学】
- 张正友标定法小结
- matlab中字符串和变量一起显示输出&eval()函数用法
- android7.0 权限获取