Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
Note:
1、The length of both num1 and num2 is < 110.
2、Both num1 and num2 contains only digits 0-9.
3、Both num1 and num2 does not contain any leading zero.
4、You must not use any built-in BigInteger library or convert the inputs to integer directly.
public class Solution {
public String multiply(String num1, String num2) {
int len1 = num1.length(),len2 = num2.length();
int[] num = new int[len1+len2];
int n = len1+len2;
for(int i=len1-1;i>=0;i--)
for(int j=len2-1;j>=0;j--)
{
num[i+j+1] += (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
}
for(int i=n-1;i>0;i--)
{
int carry = num[i]/10;
num[i] %= 10;
num[i-1] += carry;
}
String result = "";
boolean flag = false;
for(int i=0;i<n;i++)
{
if(flag == false&&num[i] == 0)
continue;
else
{
result += num[i];
flag = true;
}
}
if(result.equals(""))
return "0";
return result;
}
}