Seawolf 发表于 2019-9-7 03:20:49

leetcode 371. Sum of Two Integers

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

Example 1:

Input: a = 1, b = 2
Output: 3
Example 2:

Input: a = -2, b = 3
Output: 1

class Solution {
    public int getSum(int a, int b) {
      if(a == 0) return b;
      if(b == 0) return a;
      while(b != 0){
            
            int carry = a & b;
            a = a ^ b;
            b = carry << 1;
      }
      
      return a;
    }
}
页: [1]
查看完整版本: leetcode 371. Sum of Two Integers