A + B Problem
Question
Write a function that add two numbers A
and B
. You should not use +
or any arithmetic operators.
Example
Given a=1
and b=2
return 3
Note There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and return it.
Challenge Of course you can just return a + b to get accepted. But Can you challenge not do it like that?
Thoughts
1
a-(-b)
2
Analysis
对于python来说,因为integer不会overflow,所以这个方法不好用,carry会无限下去
Solution
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
};