赞
踩
示例 1:
输入: a = 1, b = 2
输出: 3
示例 2:
输入: a = -2, b = 3
输出: 1
class Solution {
public int getSum(int a, int b) {
int carry = a & b;
int sumNoCarry = a ^ b;
int temp1 = carry;
while(temp1 != 0){
carry = temp1 << 1;
temp1 = carry & sumNoCarry;
sumNoCarry = carry ^ sumNoCarry;
}
return sumNoCarry;
}
}
carry = carry & sumNoCarry;
sumNoCarry = carry ^ sumNoCarry;
这么写是错的,因为会把carry进位数覆盖掉,而这个数我们要用两回。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。