https://leetcode.com/problems/multiply-strings/description/
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
, 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.
我就不懂为啥自己这么一个大一就应该会的题能卡一个上午,还要看题解????
好好反省吧QAQ
class Solution {
public:
string multiply(string num1, string num2) {
string num(num1.size()+num2.size(),'0');
for(int i=num1.size()-1;i>=0;i--){
for(int j=num2.size()-1;j>=0;j--){
int pro=(num1[i]-'0')*(num2[j]-'0')+num[i+j+1]-'0';
num[i+j+1]=pro%10+'0';
num[i+j]=pro/10+num[i+j];
}
}
int e=num.find_first_not_of("0");
return e<0?"0":num.substr(e);
}
};