-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiply Strings.java
More file actions
36 lines (34 loc) · 882 Bytes
/
Multiply Strings.java
File metadata and controls
36 lines (34 loc) · 882 Bytes
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
public class Solution
{
public String multiply(String num1, String num2)
{
int len1 = num1.length();
int len2 = num2.length();
int[] dp = new int[len1 + len2];
for (int i = len1 - 1; i >= 0; i--)
{
for (int j = len2 - 1; j >= 0; j--)
{
dp[len1 + len2 - i - j - 2] += Character.getNumericValue(num1.charAt(i)) * Character.getNumericValue(num2.charAt(j));
dp[len1 + len2 - i - j - 1] += dp[len1 + len2 - i - j - 2] / 10;
dp[len1 + len2 - i - j - 2] %= 10;
}
}
StringBuilder ret = new StringBuilder();
boolean start = false;
for (int i = dp.length - 1; i >= 0; i--)
{
if (dp[i] != 0)
{
start = true;
}
if (start)
{
ret.append(dp[i]);
}
}
if (!start)
return "0";
return ret.toString();
}
}