public class Solution {
/**
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here
if(b==0)return a;
else
{
int c=a^b; //异或运算
int d=(a&b)<<1; //与运算,进位
return aplusb(c,d);
}
}
}
本文介绍了一种使用异或和与运算实现整数加法的方法,通过递归调用自身来处理进位,避免了直接使用加号进行计算。
3217

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



