每天一道LeetCode--371. Sum of Two Integers
alculate 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.
注意:不能使用运算符喽
那我们采用异或移位操作
public class Solution { public int getSum(int a, int b) { int sum=0,carry=0; do{ sum=a^b; carry=(a&b)<<1; a=sum; b=carry; }while(carry!=0); return sum; } }