Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

两整数之和 #33

Open
Aras-ax opened this issue Apr 17, 2019 · 0 comments
Open

两整数之和 #33

Aras-ax opened this issue Apr 17, 2019 · 0 comments

Comments

@Aras-ax
Copy link
Owner

Aras-ax commented Apr 17, 2019

不使用运算符 + 和 - ,计算两整数 a 、b 之和。

示例 1:

输入: a = 1, b = 2
输出: 3

示例 2:

输入: a = -2, b = 3
输出: 1

解答

function getSum(a, b) {
    if (a === 0 || b === 0) {
        return a ^ b;
    }
    // 处理进位操作
    return getSum(a ^ b, (a & b) << 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant