https://leetcode.com/problems/multiply-strings/
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
public class Solution {
public String multiply(String num1, String num2) {
if("0".equals(num1) || "0".equals(num2)) {
return "0";
}
int l1 = num1.length(), l2 = num2.length();
int idx = l1+l2, carry = 0;
char[] ans = new char[idx];
Arrays.fill(ans, '0');
for(int i=l1-1; i>=0; --i) {
for(int j=l2-1; j>=0; --j) {
int n = ans[idx-l2+j]-'0'+(num1.charAt(i)-'0')*(num2.charAt(j)-'0')+carry;
ans[idx-l2+j] = (char)(n%10+'0');
carry = n/10;
}
if(carry > 0) {
ans[idx-l2-1] = (char)(ans[idx-l2-1]+carry);
carry = 0;
}
--idx;
}
idx = 0;
while('0' == ans[idx]) ++idx;
return new String(ans, idx, l1+l2-idx);
}
}