Leetcode 371. Sum of Two Integers (Easy) (cpp)

来源:互联网 发布:c语言课件 编辑:程序博客网 时间:2024/05/18 02:15

Leetcode 371. Sum of Two Integers (Easy) (cpp)

Tag: Bit Manipulation

Difficulty: Easy


/*371. Sum of Two Integers (Easy)Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.*/class Solution {public:    int getSum(int a, int b) {        while (b){            int x = a ^ b, y = (a & b) << 1;            a = x; b = y;        }        return a;    }};


0 0
原创粉丝点击