371 Sum of Two Integers 两整数之和
不使用运算符 + 和-,计算两整数a 、b之和。
示例:
若 a = 1 ,b = 2,返回 3。
详见:https://leetcode.com/problems/sum-of-two-integers/description/
C++:
class Solution {
public:
int getSum(int a, int b) {
while(b)
{
int tmp=a^b;
b=(a&b)<<1;
a=tmp;
}
return a;
}
};
参考:https://www.cnblogs.com/grandyang/p/5631814.html
浙公网安备 33010602011771号