-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMultiply Strings.java
35 lines (31 loc) · 1.24 KB
/
Multiply Strings.java
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
/*
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
*/
public class Solution {
public String multiply(String num1, String num2) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (num1 == null || num2 == null) return null;
int length1 = num1.length();
int length2 = num2.length();
int[] result = new int[length1 + length2];
for (int i = 0; i < length1; i++) {
int a = num1.charAt(length1 - i - 1) - '0';
int carry = 0;
for (int j = 0; j < length2; j++) {
int b = num2.charAt(length2 - j - 1) - '0';
result[i + j] += a * b + carry;
carry = result[i + j] / 10;
result[i + j] %= 10;
}
result[i + length2] += carry;
}
StringBuilder s = new StringBuilder();
for (int i = length1 + length2 - 1; i >= 0; i--) {
if (i != 0 && result[i] == 0 && s.length() == 0) continue;
s.append(result[i]);
}
return s.toString();
}
}