File tree 1 file changed +28
-0
lines changed
solution/0043.Multiply Strings
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments