【Leetcode】Sum of Two Integers

题目链接:https://leetcode.com/problems/sum-of-two-integers/

题目:
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.

思路:
唉,这题虽然是easy,但是真好烦的一题,之前在hihocoder(还是其他oj)上好像也做过这题? 每次都是模拟32位运算然后遇到负数就搞不定了。。。参考别人的做法。

算法:

     public int getSum(int a, int b) {
       int c = 0;
       while(b!=0){
           c= a^b; //add
           b = (a&b)<<1;//carry
           a =c;
       }
       return a;
    }

你可能感兴趣的:(LeetCode)