天天一道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 -.spa

Example:
Given a = 1 and b = 2, return 3.code

 注意:不能使用運算符嘍blog

那咱們採用異或移位操做get

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;
    }
}
相關文章
相關標籤/搜索