leet-code/src/main/java/leetcode/editor/cn/SumOfTwoIntegers.java
huangge1199@hotmail.com e82f1334c6 371:两整数之和
2021-05-10 23:16:28 +08:00

39 lines
851 B
Java

//不使用运算符 + 和 - ,计算两整数 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)
}