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.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
高精度乘法。需要注意的是char表示的数字和int对应的数字之间的转化。
class Solution { public: string multiply(string num1, string num2) { int l1=num1.length(),l2=num2.length(); if(l1==0||l2==0){ string ans=""; return ans; } string ans(l1+l2,'0'); for(int i=l1-1;i>=0;i--){ int add=0; for(int j=l2-1;j>=0;j--){ int t=(ans[i+j+1]-'0')+(num1[i]-'0')*(num2[j]-'0')+add; ans[i+j+1]=t%10+'0'; add=t/10; } ans[i]=add+'0'; } size_t s0 = ans.find_first_not_of('0'); if(s0!=string::npos){ return ans.substr(s0); } return "0"; } };