371:两整数之和

This commit is contained in:
huangge1199@hotmail.com 2021-05-10 23:16:28 +08:00
parent 8a4171b087
commit e82f1334c6
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,39 @@
//不使用运算符 + - 计算两整数 a b 之和
//
// 示例 1:
//
// 输入: a = 1, b = 2
//输出: 3
//
//
// 示例 2:
//
// 输入: a = -2, b = 3
//输出: 1
// Related Topics 位运算
// 👍 393 👎 0
package leetcode.editor.cn;
//371:两整数之和
public class SumOfTwoIntegers {
public static void main(String[] args) {
//测试代码
Solution solution = new SumOfTwoIntegers().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int getSum(int a, int b) {
while(b != 0){
int temp = a ^ b;
b = (a & b) << 1;
a = temp;
}
return a;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,13 @@
<p><strong>不使用</strong>运算符&nbsp;<code>+</code>&nbsp;<code>-</code>&nbsp;​​​​​​​,计算两整数&nbsp;<code>a</code>&nbsp;<code>b</code>&nbsp;​​​​​​​之和。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入: </strong>a = 1, b = 2
<strong>输出: </strong>3
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入: </strong>a = -2, b = 3
<strong>输出: </strong>1</pre>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 393</li><li>👎 0</li></div>