File tree 1 file changed +15
-9
lines changed
src/main/java/com/fishercoder/solutions 1 file changed +15
-9
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
- /**Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
3
+ /**
4
+ * 371. Sum of Two Integers
5
+ *
6
+ * Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
4
7
5
8
Example:
6
- Given a = 1 and b = 2, return 3.*/
9
+ Given a = 1 and b = 2, return 3.
10
+ */
7
11
public class _371 {
8
12
9
- /**reference: http://stackoverflow.com/questions/9070937/adding-two-numbers-without-operator-clarification*/
10
- public int getSum (int a , int b ) {
11
- if (b == 0 ) {
12
- return a ;
13
+ public static class Solution1 {
14
+ /** reference: http://stackoverflow.com/questions/9070937/adding-two-numbers-without-operator-clarification */
15
+ public int getSum (int a , int b ) {
16
+ if (b == 0 ) {
17
+ return a ;
18
+ }
19
+ int sum = a ^ b ;
20
+ int carry = (a & b ) << 1 ;
21
+ return getSum (sum , carry );
13
22
}
14
- int sum = a ^ b ;
15
- int carry = (a & b ) << 1 ;
16
- return getSum (sum , carry );
17
23
}
18
24
}
You can’t perform that action at this time.
0 commit comments