首页 > 代码库 > [leetcode] 371. Sum of Two Integers 解题报告

[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 );    }

 

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