Skip to content

Commit 36ef4d8

Browse files
committed
Shorten solution of AddStrings task
1 parent 0f7067f commit 36ef4d8

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

src/main/java/by/andd3dfx/numeric/AddStrings.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,15 @@ public static String addStrings(String num1, String num2) {
3535

3636
var sb = new StringBuilder();
3737
var accumulator = 0;
38-
while (i >= 0 && j >= 0) {
39-
int n1 = chars1[i] - '0';
40-
int n2 = chars2[j] - '0';
38+
while (i >= 0 || j >= 0 || accumulator > 0) {
39+
int n1 = (i >= 0) ? (chars1[i] - '0') : 0;
40+
int n2 = (j >= 0) ? (chars2[j] - '0') : 0;
4141

4242
accumulator = calcAccumulator(n1 + n2 + accumulator, sb);
4343

4444
i--;
4545
j--;
4646
}
47-
while (i >= 0) {
48-
int n1 = chars1[i] - '0';
49-
accumulator = calcAccumulator(n1 + accumulator, sb);
50-
i--;
51-
}
52-
while (j >= 0) {
53-
int n2 = chars2[j] - '0';
54-
accumulator = calcAccumulator(n2 + accumulator, sb);
55-
j--;
56-
}
57-
if (accumulator > 0) {
58-
sb.append(accumulator);
59-
}
6047

6148
return sb.reverse().toString();
6249
}

0 commit comments

Comments
 (0)