首页 > 代码库 > LeetCode之371. Sum of Two Integers

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/

LeetCode之371. Sum of Two Integers