Skip to content

Latest commit

 

History

History
59 lines (38 loc) · 941 Bytes

README_EN.md

File metadata and controls

59 lines (38 loc) · 941 Bytes

中文文档

Description

Write a function that adds two numbers. You should not use + or any arithmetic operators.

Example:

Input: a = 1, b = 1

Output: 2

 

Note:

  • a and b may be 0 or negative.
  • The result fits in 32-bit integer.

Solutions

Python3

Java

class Solution {
    public int add(int a, int b) {
        int sum = 0, carry = 0;
        while (b != 0) {
            sum = a ^ b;
            carry = (a & b) << 1;
            a = sum;
            b = carry;
        }
        return a;
    }
}

...