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

 

posted @ 2018-06-06 22:01  約束の空  阅读(87)  评论(0)    收藏  举报