# 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

示例 1:

输入: num1 = "2", num2 = "3"
输出:
"6"

示例 2:

输入: num1 = "123", num2 = "456"
输出:
"56088"

说明:

  1. num1 和 num2 的长度小于110。
  2. num1 和 num2 只包含数字 0-9
  3. num1 和 num2 均不以零开头,除非是数字 0 本身。
  4. 不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理

以下错误的选项是?

## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp int main() { Solution sol; string num1 = "2", num2 = "3"; string res; res = sol.multiply(num1, num2); cout << res; return 0; } ``` ## 答案 ```cpp class Solution { public: string multiply(string num1, string num2) { if (num1 == "0" || num2 == "0") return "0"; int size1 = num1.size(), size2 = num2.size(); string str(size1 + size2, '0'); for (int i = size2 - 1; i >= 0; --i) { int mulflag = 0, addflag = 0; for (int j = size1 - 1; j >= 0; --j) { int temp1 = (num2[i] - '0') * (num1[j] - '0') + mulflag; mulflag = temp1 / 10; temp1 = temp1 % 10; int temp2 = str[i + j + 1] - '0' + temp1 + addflag; str[i + j] = temp2 % 10 + '0'; addflag = temp2 / 10; } str[i] += mulflag + addflag; } if (str[0] == '0') str = str.substr(1, str.size()); return str; } }; ``` ## 选项 ### A ```cpp class Solution { public: string multiply(string num1, string num2) { string res(num1.length() + num2.length(), '0'); for (int i = num2.length() - 1; i >= 0; i--) { int j, carry = 0; for (j = num1.length() - 1; j >= 0; j--) { carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0'); res[i + j + 1] = carry % 10 + '0'; carry /= 10; } res[i + j + 1] = carry + '0'; } int i; for (i = 0; i < res.length() - 1; i++) { if (res[i] != '0') { break; } } return res.substr(i); } }; ``` ### B ```cpp class Solution { public: string multiply(string num1, string num2) { if (num1 == "0" || num2 == "0") { return "0"; } int n1 = num1.size(), n2 = num2.size(); int n = n1 + n2; vector ves(n, 0); int k = n - 2; for (int i = 0; i < n1; i++) for (int j = 0; j < n2; j++) { ves[k - i - j] += (num1[i] - '0') * (num2[j] - '0'); } int carry = 0; for (int i = 0; i < n; i++) { ves[i] += carry; carry = ves[i] / 10; ves[i] %= 10; } int t = n - 1; while (ves[t] == 0) { --t; } string result; while (t >= 0) { result.append(1, ves[t] + '0'); --t; } return result; } }; ``` ### C ```cpp class Solution { public: string multiply(string num1, string num2) { string &upper = num1; string &down = num2; deque res, adv; for (int i = 0; i < down.length(); i++) { if (!res.empty()) { for (int k = 0; k < upper.length() - 1; k++) { adv.push_back(res.back()); res.pop_back(); } } for (int j = 0; j < upper.length(); j++) { if (adv.empty()) { res.push_back( (down[i] - '0') * (upper[j] - '0')); } else { res.push_back( adv.back() + (down[i] - '0') * (upper[j] - '0')); adv.pop_back(); } } } deque::iterator it = res.end() - 1; while (it != res.begin() - 1) { if (*it > 9) { if (it == res.begin()) { int tmp = *it; *it = tmp % 10; res.push_front(tmp / 10); } else { int tmp = *it; *it = tmp % 10; *(it - 1) += tmp / 10; } } it--; } string _r; for (it = res.begin(); it != res.end(); it++) { _r += (*it + '0'); } return _r; } }; ```