Leetcode

Leetcode 371. Sum of Two Integer

2018-09-05  本文已影响2人  SnailTyan

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Sum of Two Integers

2. Solution

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;;
    }
};
class Solution {
public:
    int getSum(int a, int b) {
        return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
    }
};

Reference

  1. https://leetcode.com/problems/sum-of-two-integers/description/
上一篇下一篇

猜你喜欢

热点阅读