[leetcode] 371. Sum of Two Integers 解题报告

Calculate 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.

用 异或 表示sum,与 表示 carry,carry左移一位后,递归调用getSum(sum,carry<<1)

一刷:

    public int getSum(int a, int b) {
        return b ==0 ? a:getSum( a^b , (a&b)<<1 );
    }

 

posted @ 2016-09-04 22:02  pulusite  阅读(117)  评论(0编辑  收藏  举报