Fork me on GitHub

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/

posted @ 2016-10-24 02:20  CC11001100  阅读(132)  评论(0编辑  收藏  举报