Files
2020-08-09 00:39:24 +08:00

27 lines
1.0 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [371. 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 1:**
Input: a = 1, b = 2
Output: 3
**Example 2:**
Input: a = -2, b = 3
Output: 1
## 题目大意
不使用运算符 + 和 - ​​​​​​​,计算两整数 a 、b ​​​​​​​之和。
## 解题思路
- 要求不用加法和减法运算符计算 `a+b`。这一题需要用到 `^``&` 运算符的性质,两个数 ^ 可以实现两个数不带进位的二进制加法。这里需要实现加法,肯定需要进位。所以如何找到进位是本题的关键。
- 在二进制中,只有 1 和 1 加在一起才会进位0 和 00 和 11 和 0这三种情况都不会进位规律就是 `a & b` 为 0 的时候就不用进位,为 1 的时候代表需要进位。进位是往前进一位,所以还需要左移操作,所以加上的进位为 `(a&b)<<1`