就像正常的乘法一样 存下来结果的每一位 存成一个int array 然后处理一下大于十的 累加到前一位
然后就concat成string 但是注意前面很多0 不能加 但是假如全是0 就要返回0
public class Solution {
public String multiply(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
int [] products = new int [ len1 + len2 ];
for ( int i = len1 - 1; i >= 0; i -- ){
for ( int j = len2 - 1; j >= 0; j -- ){
products[ i + j + 1 ] += ( num1.charAt(i) - '0' ) * ( num2.charAt(j) - '0' );
}
}
int carry = 0;
for ( int i = products.length - 1; i >= 0; i -- ){
products[ i ] += carry;
carry = products[i] / 10;
products[ i ] %= 10;
}
StringBuilder sb = new StringBuilder ();
if ( carry != 0 )
sb.append ( Integer.toString(carry) );
int pos = 0;
while ( pos < products.length && products[pos] == 0 ){
pos ++;
}
if ( pos == products.length )
return "0";
for (; pos < products.length; pos ++ ){
sb.append ( Integer.toString(products[pos]) );
}
return sb.toString();
}
}