371. Sum of Two Integers

Posted 約束の空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了371. Sum of Two Integers相关的知识,希望对你有一定的参考价值。

不能用加减法,想到用位运算解题。

本题思路可见 http://www.cnblogs.com/grandyang/p/5451942.html

关于位运算部分总结 可见 https://blog.csdn.net/fly_yr/article/details/51144272

class Solution {
public:
    int getSum(int a, int b) {
        int sum=a;
        while(b){
            sum = a^b;
            int carry= (a&b)<<1;
            a = sum;
            b = carry;
        }
        return sum;
    }
};

 

以上是关于371. Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章

371. Sum of Two Integers

371. Sum of Two Integers

LeetCode(371) Sum of Two Integers

371. Sum of Two Integers

371. Sum of Two Integers

371. Sum of Two Integers