题目链接
英文链接:https://leetcode.com/problems/sum-of-two-integers/
中文链接:https://leetcode-cn.com/problems/sum-of-two-integers/
题目详述
不使用运算符 +
和 -
,计算两整数 a
、b
之和。
示例 1:
1 | 输入: a = 1, b = 2 |
示例 2:
1 | 输入: a = -2, b = 3 |
题目详解
- 模拟竖式加法直至进位为 0。
a ^ b
是没有进位的和,(a & b) << 1
是进位。
1 | public class LeetCode_00371 { |
也可以改为迭代形式。
1 | public class LeetCode_00371 { |