LeetCode之371. Sum of Two Integers

----------------------------------

使用位运算实现加法:

a^b 加不同部分
(a&b)<<1 加相同部分
递归相加

AC代码:

public class Solution {
    public int getSum(int a, int b) {
        if(b==0) return a;
        int t1=a^b;
        int t2=(a&b)<<1;
        return getSum(t1,t2);
    }
}

题目来源: https://leetcode.com/problems/sum-of-two-integers/

原文地址:https://www.cnblogs.com/cc11001100/p/5991638.html