题目描述
lintcode a+b 问题
题目来源及自己的思路
代码def aplusb(self, a, b):
# write your code here
while True:
a, b = a^b, (a&b)<<1
if a==0 or b == 0: return a or b
当 a=-b 时 为什么代码会超时, 而同样的逻辑,用Java就不会超时
public int aplusb(int a ,int b) { // write your code here, try to do it without arithmetic operators.
while(true){ int x = a^b; //记录不进位数
int y = (a&b)<<1; //记录进位数
if(x==0){ return y;
} if (y==0){ return x;
}
a=x;
b=y;
} // while
}