Skip to content

Commit c766fe4

Browse files
refactor 371
1 parent 7ec5460 commit c766fe4

File tree

1 file changed

+15
-9
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-9
lines changed
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package com.fishercoder.solutions;
22

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 -.
47
58
Example:
6-
Given a = 1 and b = 2, return 3.*/
9+
Given a = 1 and b = 2, return 3.
10+
*/
711
public class _371 {
812

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);
1322
}
14-
int sum = a ^ b;
15-
int carry = (a & b) << 1;
16-
return getSum(sum, carry);
1723
}
1824
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy