您的位置:首页 > 其它

LeetCode:Sum of Two Integers

2016-07-10 23:46 453 查看
一、问题描述

Calculate the sum of two integers a and b,
but you are not allowed to
use the operator 
+
 and 
-
.

二、解决思路

两个数的相加用异或,进位用与。

三、代码

class Solution {

public:

    int getSum(int a, int b) {

        while(b){

            int c = a & b;

            a = a ^ b;

            b = c << 1;

        }

        return a;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode