forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyStrings.java
More file actions
33 lines (31 loc) · 1.12 KB
/
Copy pathMultiplyStrings.java
File metadata and controls
33 lines (31 loc) · 1.12 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
/*
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.
*/
// [1] the length of digits in the result may be M+N-1, or M+N
// [2] Exception to he above formula: num1=="0" or num2=="0"
// time: O(m*n); space: O(m+n)
public class Solution {
public String multiply(String num1, String num2) {
if (num1==null || num2==null)
return num1==null?num2:num1;
int M = num1.length(), N = num2.length();
if (num1.equals("0") || num2.equals("0"))
return "0";
int[] res = new int[M+N-1];
for (int i=0; i<M; i++){
for (int j=0; j<N; j++){
res[i+j] += (num1.charAt(i)-'0')*(num2.charAt(j)-'0'); // Note, += here, not =
}
}
int carry = 0;
StringBuilder sb = new StringBuilder();
for (int i=res.length-1; i>=0; i--){
sb.append((carry+res[i])%10);
carry = (carry+res[i])/10;
}
if (carry>0)
sb.append(carry);
return sb.reverse().toString();
}
}