371:两整数之和
This commit is contained in:
parent
8a4171b087
commit
e82f1334c6
39
src/main/java/leetcode/editor/cn/SumOfTwoIntegers.java
Normal file
39
src/main/java/leetcode/editor/cn/SumOfTwoIntegers.java
Normal 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)
|
||||||
|
|
||||||
|
}
|
13
src/main/java/leetcode/editor/cn/SumOfTwoIntegers.md
Normal file
13
src/main/java/leetcode/editor/cn/SumOfTwoIntegers.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<p><strong>不使用</strong>运算符 <code>+</code> 和 <code>-</code> ,计算两整数 <code>a</code> 、<code>b</code> 之和。</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>
|
Loading…
Reference in New Issue
Block a user