-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiply.java
37 lines (29 loc) · 973 Bytes
/
Multiply.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
36
37
class Multiply {
public String multiply(String num1, String num2) {
StringBuilder sb = new StringBuilder();
if(num1.equals("0") || num2.equals("0")) {
return "0";
}
char[] n1 = num1.toCharArray();
char[] n2 = num2.toCharArray();
int[] res = new int[n1.length + n2.length];
for(int i = n1.length - 1; i >= 0; i--) {
for(int j = n2.length - 1; j >= 0; j--) {
res[i + j + 1] += (n1[i] - '0') * (n2[j] - '0');
}
}
int carry = 0;
for(int i = res.length - 1; i >= 0; i--) {
int sum = res[i] + carry;
res[i] = sum % 10;
carry = sum / 10;
}
if(res[0] != 0) {
sb.append(res[0]);
}
for(int i = 1; i < res.length; i++) {
sb.append(res[i]);
}
return sb.toString();
}
}