Skip to content

Commit 8228d8f

Browse files
43. Multiply Strings (java)
1 parent 3899f9e commit 8228d8f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String multiply(String num1, String num2) {
3+
char[] chars1 = num1.toCharArray(),chars2 = num2.toCharArray();
4+
int[] result = new int[chars1.length+chars2.length];
5+
int pow = result.length-1;
6+
for (int i = chars1.length - 1; i >= 0; i--) {
7+
int a = chars1[i] - '0';
8+
int j = 0,bit = pow;
9+
for (int i1 = chars2.length - 1; i1 >= 0; i1--) {
10+
int b = chars2[i1] -'0';
11+
j = a*b + j + result[bit];
12+
result[bit--] = j%10;
13+
j = j/10;
14+
}
15+
while (j!=0){
16+
j += result[bit];
17+
result[bit--] = j%10;
18+
j = j/10;
19+
}
20+
pow--;
21+
}
22+
StringBuilder builder = new StringBuilder();
23+
int i = 0;
24+
for (; i < result.length; i++) if (result[i] != 0) break;
25+
for (; i < result.length; i++) builder.append(result[i]);
26+
return builder.length()==0? "0" : builder.toString();
27+
}
28+
}

0 commit comments

Comments
 (0)