43. 字符串相乘
题目描述
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.
思路
模拟乘法的运算过程,两个非负数相乘,结果的位数至多为这两个非负数位数之和.不妨设这两个非负数位数分别为x
,y
,取这两个位数的最大的两个数相乘即(10^x-1)(10^y-1)=10^(x+y)-10^x-10^y+1
,而10^(x+y)
为最小的x+y+1
位数,所以结果为一个x+y
位数.
代码实现
class Solution {
public:
string multiply(string num1, string num2) {
if(num1.length()==0 || num2.length()==0)
return "";
string ret(num1.size()+num2.size(),'0');
for(int i=num2.size()-1;i>=0;--i)
{
int carry = 0;
//位置对应关系利用平移不变性
//它最初位于ret中的位置为:num1.size()+num2.size()-1-(num2.size()-1-i)
int k = num1.size()+num2.size()-1-(num2.size()-1-i);//初始化指针指向结果中的位置
for(int j=num1.size()-1;j>=0;--j)
{
int sum = (num2[i]-'0')*(num1[j]-'0')+(ret[k]-'0')+carry;
ret[k] = sum%10 + '0';
carry = sum/10;
k--;
}
while(carry)
{
int sum = (ret[k]-'0')+carry;
ret[k] = sum%10 + '0';
carry = sum/10;
k--;
}
}
int i = 0;
while(i<ret.size() && ret[i]=='0') ++i;
return i==ret.size()?"0":ret.substr(i);
} //例如123456*0=0
};