-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43. Multiply Strings.java
More file actions
40 lines (25 loc) · 1.13 KB
/
43. Multiply Strings.java
File metadata and controls
40 lines (25 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) return "0";
int[] result = new int[num1.length() + num2.length()];
for (int i = num1.length() - 1; i >= 0; i--) {
for (int j = num2.length() - 1; j >= 0; j--) {
int digit1 = num1.charAt(i) - '0';
int digit2 = num2.charAt(j) - '0';
int mul = digit1 * digit2;
int posLow = i + j + 1;
int posHigh = i + j;
int sum = mul + result[posLow];
result[posLow] = sum % 10;
result[posHigh] += sum / 10;
}
}
StringBuilder product = new StringBuilder();
for (int num : result) {
if (!(product.length() == 0 && num == 0)) {
product.append(num);
}
}
return product.length() == 0 ? "0" : product.toString();
}
}