Sum of Two Integers

2019-05-25 22:48:17 浏览数 (1)

1. Description

2. Solution

  • Version 1
代码语言:javascript复制
class Solution {
public:
    int getSum(int a, int b) {
        int sum = 0;
        int carry = 0;
        while(b)
        {
            sum = a ^ b; 
            carry = a & b;
            a = sum;
            b = carry << 1;
        }
        return sum;;
    }
};
  • Version 2
代码语言:javascript复制
class Solution {
public:
    int getSum(int a, int b) {
        return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
    }
};

0 人点赞