LintCode 1. A + B 问题 ☆

来源:互联网 发布:oppor11t网络切换破解 编辑:程序博客网 时间:2024/06/07 09:30

代码:

public class Solution {    /*     * @param : An integer     * @param : An integer     * @return: The sum of a and b     */    public int aplusb(int a, int b) {        // write your code here        // 方法一:return a-(b)*(-1);        // 方法二:        if(b == 0)        return a;        int XORresult = a ^ b;        int carry = (a & b)<<1;        return aplusb(XORresult,carry);    }};

方法一:用两个负号
方法二:异或来代替加法 注意处理进位