题目
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.
难度: Easy
实现
public class Solution {
public int getSum(int a, int b){
int sum = a;
while (b != 0){
sum = a^b;
b = (a & b) << 1;
a = sum;
}
return sum;
}
}
本文介绍了一种特殊的算法,用于在不使用加法和减法运算符的情况下计算两个整数的和。通过位操作实现了这一目标,提供了一个具体的Java代码示例。
756

被折叠的 条评论
为什么被折叠?



