https://leetcode.com/problems/multiply-strings/
Given two non-negative integers
num1
andnum2
represented as strings, return the product ofnum1
andnum2
, also represented as a string.Example 1:
Input: num1 = "2", num2 = "3" Output: "6"Example 2:
Input: num1 = "123", num2 = "456" Output: "56088"Note:
- The length of both
num1
andnum2
is < 110.- Both
num1
andnum2
contain only digits0-9
.- Both
num1
andnum2
do not contain any leading zero, except the number 0 itself.- You must not use any built-in BigInteger library or convert the inputs to integer directly.
最初是的想法是取乘数的每一位和被乘数相乘,然后将结果累加起来,累加可原地操作来减少开销
class Solution {
public:
string multiply(string num1, string num2) {
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
//"9133" "0" -> "0000"
if(num1 == "0" || num2 == "0") return "0";
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
// cout << num1 << endl;
// cout << num2 << endl;
string result = multip_1_digit(num1, num2[0]-'0');
cout << result << endl;
for(int i = 1; i < num2.size(); ++i){
string tem = multip_1_digit(num1, num2[i]-'0');
// result = add_2_nums(result, tem, i);
add_2_nums(result, tem, i);
// cout << "char: " << num2[i] << endl;
// cout << "cur: " << tem << endl;
// add_2_nums(result, tem, i);
// cout << "result: " << result << endl;
}
reverse(result.begin(), result.end());
return result;
}
string multip_1_digit(string num1, int num2){
string result;
int car = 0;
int tem;
for(int i = 0; i < num1.size(); ++i){
tem = (num1[i]-'0')*num2+car;
car = tem/10;
tem = tem%10;
result.push_back((char)('0'+tem));
}
if(car != 0){
result.push_back((char)('0'+car));
}
return result;
}
void add_2_nums(string &num1, string num2, int st){
int cur1 = st;
int cur2 = 0;
int car = 0;
int tem;
// string result = num1.substr(0, st);
// cout << result << endl;
while(cur1 < num1.size() && cur2 < num2.size()){
tem = (int)(num1[cur1]-'0') + (int)(num2[cur2]-'0') + car;
car = tem/10;
tem = tem%10;
// result.push_back((char)('0'+tem));
num1[cur1] = (char)('0'+tem);
++cur1;
++cur2;
}
// cout << result << endl;
while(cur1 < num1.size()){
if(car == 0){
// result.append(num1.substr(cur1));
break;
}
tem = (int)(num1[cur1]-'0')+ car;
car = tem/10;
tem = tem%10;
// result.push_back((char)('0'+tem));
num1[cur1] = (char)('0'+tem);
++cur1;
}
while(cur2 < num2.size()){
if(car == 0){
// result.append(num2.substr(cur2));
num1.append(num2.substr(cur2));
break;
}
tem = (int)(num2[cur2]-'0')+ car;
car = tem/10;
tem = tem%10;
// result.push_back((char)('0'+tem));
num1.push_back((char)('0'+tem));
++cur2;
}
// cout << result << endl;
if(car != 0){
// result.push_back((char)('0'+car));
num1.push_back((char)('0'+car));
}
// return result;
}
};
注意到加法可以不用每次都加,可以将所有的中间结果记录下来然后一次性加起来,但是位数对齐的问题很复杂,而且接下来我们会发现在成复杂度的原因不在于运算本身
上述解法中时间开销的主要来源在于多次的push_back和append操作造成空间的重分配,所以最好的方法就是:1、直接在每个string初始化的时候就声明空间大小 2、尽量减少中间变量的string数量,直接在result变量上原地操作
同时在代码逻辑中可以通过倒叙遍历的方法,避免了reverse函数的调用
class Solution {
public:
string multiply(string num1, string num2) {
string result(num1.size()+num2.size(), '0');
int car;
for(int i = num1.size()-1; i >= 0; --i){
car = 0;
for(int j = num2.size()-1; j >= 0; --j){
int tem = (result[i+j+1]-'0') + (num1[i]-'0')*(num2[j]-'0') + car;
car = tem/10;
result[i+j+1] = (char)(tem%10 + '0');
}
result[i] += car; //result[i+(-1)+1]
}
int beg = 0;
while(beg < result.size() && result[beg] == '0') ++beg;
cout << beg;
if(beg == result.size()) return "0";
else return result.substr(beg);
}
};
上述代码中的最大难点在于确定num1[i] num2[j]后算出的结果在result中对应的索引的确定,可以这么理解:
num1中索引i 对应数字1中的第num1.size()-i位(从低到高),同理num2[j]对应数字2中的第num2.size()-j位,所以乘积对应结果数字中的第(num1.size()-i+num2.size()-j+1)位,由于在result中存储的数字是右侧顶格的,所以对应的索引为(num1.size()+num2.size()-(num1.size()-i+num2.size()-j+1)) = (i+j+1)