1. A + B 问题

描述
中文
English
给出两个整数 aa 和 bb , 求他们的和。

你不需要从输入流读入数据,只需要根据aplusb的两个参数a和b,计算他们的和并返回就行。

您在真实的面试中是否遇到过这个题?  
说明
a和b都是 32位 整数么?

是的
我可以使用位运算符么?

当然可以
样例
样例  1:
	输入:  a = 1, b = 2
	输出: 3
	
	样例解释: 
	返回a+b的结果.

样例 2:
	输入:  a = -1, b = 1
	输出: 0
	
	样例解释: 
	返回a+b的结果.

挑战
显然你可以直接 return a + b,但是你是否可以挑战一下不这样做?(不使用++等算数运算符)

相关题目
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
        return (b == 0) ? a : aplusb(a ^ b, (a & b) << 1);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39540389/article/details/88954293