Skip to content

Commit 81d4199

Browse files
refactor 415
1 parent cff44e3 commit 81d4199

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 415. Add Strings
5+
*
46
* Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
57
68
Note:
79
8-
The length of both num1 and num2 is < 5100.
9-
Both num1 and num2 contains only digits 0-9.
10-
Both num1 and num2 does not contain any leading zero.
11-
You must not use any built-in BigInteger library or convert the inputs to integer directly.
10+
1. The length of both num1 and num2 is < 5100.
11+
2. Both num1 and num2 contains only digits 0-9.
12+
3. Both num1 and num2 does not contain any leading zero.
13+
4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
1214
*/
1315
public class _415 {
1416

15-
public static String addStrings(String num1, String num2) {
16-
if (num1 == null || num1.length() == 0) {
17-
return num2;
18-
} else if (num2 == null || num2.length() == 0) {
19-
return num1;
20-
}
17+
public static class Solution1 {
18+
public String addStrings(String num1, String num2) {
19+
if (num1 == null || num1.length() == 0) {
20+
return num2;
21+
} else if (num2 == null || num2.length() == 0) {
22+
return num1;
23+
}
2124

22-
int i = num1.length() - 1;
23-
int j = num2.length() - 1;
24-
long carry = 0;
25-
long sum = 0;
26-
StringBuilder sb = new StringBuilder();
27-
char[] char1 = num1.toCharArray();
28-
char[] char2 = num2.toCharArray();
29-
while (i >= 0 || j >= 0) {
30-
sum = carry;
31-
if (i >= 0) {
32-
sum += Character.getNumericValue(char1[i--]);
25+
int i = num1.length() - 1;
26+
int j = num2.length() - 1;
27+
long carry = 0;
28+
long sum = 0;
29+
StringBuilder sb = new StringBuilder();
30+
char[] char1 = num1.toCharArray();
31+
char[] char2 = num2.toCharArray();
32+
while (i >= 0 || j >= 0) {
33+
sum = carry;
34+
if (i >= 0) {
35+
sum += Character.getNumericValue(char1[i--]);
36+
}
37+
if (j >= 0) {
38+
sum += Character.getNumericValue(char2[j--]);
39+
}
40+
carry = sum / 10;
41+
sb.append(sum % 10);
3342
}
34-
if (j >= 0) {
35-
sum += Character.getNumericValue(char2[j--]);
43+
if (carry != 0) {
44+
sb.append(carry);
3645
}
37-
carry = sum / 10;
38-
sb.append(sum % 10);
39-
}
40-
if (carry != 0) {
41-
sb.append(carry);
42-
}
4346

44-
return sb.reverse().toString();
47+
return sb.reverse().toString();
48+
}
4549
}
4650

4751
}

src/test/java/com/fishercoder/_415Test.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77

88
import static junit.framework.Assert.assertEquals;
99

10-
/**
11-
* Created by fishercoder on 1/8/17.
12-
*/
1310
public class _415Test {
14-
private static _415 test;
11+
private static _415.Solution1 solution1;
1512
private static String expected;
1613
private static String actual;
1714
private static String num1;
1815
private static String num2;
1916

2017
@BeforeClass
2118
public static void setup() {
22-
test = new _415();
19+
solution1 = new _415.Solution1();
2320
expected = new String();
2421
actual = new String();
2522
num1 = new String();
@@ -40,7 +37,7 @@ public void test1() {
4037
num1 = "123";
4138
num2 = "34567";
4239
expected = "34690";
43-
actual = test.addStrings(num1, num2);
40+
actual = solution1.addStrings(num1, num2);
4441
assertEquals(expected, actual);
4542

4643
}
@@ -51,7 +48,7 @@ public void test2() {
5148
num1 = "1";
5249
num2 = "9";
5350
expected = "10";
54-
actual = test.addStrings(num1, num2);
51+
actual = solution1.addStrings(num1, num2);
5552
assertEquals(expected, actual);
5653

5754
}
@@ -62,7 +59,7 @@ public void test3() {
6259
num1 = "9";
6360
num2 = "99";
6461
expected = "108";
65-
actual = test.addStrings(num1, num2);
62+
actual = solution1.addStrings(num1, num2);
6663
assertEquals(expected, actual);
6764

6865
}

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