solution.md 1.7 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
# 字符串相乘

<p>给定两个以字符串形式表示的非负整数&nbsp;<code>num1</code>&nbsp;&nbsp;<code>num2</code>,返回&nbsp;<code>num1</code>&nbsp;&nbsp;<code>num2</code>&nbsp;的乘积,它们的乘积也表示为字符串形式。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> num1 = &quot;2&quot;, num2 = &quot;3&quot;<strong><br />输出:</strong> &quot;6&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> num1 = &quot;123&quot;, num2 = &quot;456&quot;<strong><br />输出:</strong> &quot;56088&quot;</pre><p><strong>说明:</strong></p><ol>	<li><code>num1</code>&nbsp;&nbsp;<code>num2</code>&nbsp;的长度小于110。</li>	<li><code>num1</code>&nbsp;<code>num2</code> 只包含数字&nbsp;<code>0-9</code></li>	<li><code>num1</code>&nbsp;<code>num2</code>&nbsp;均不以零开头,除非是数字 0 本身。</li>	<li><strong>不能使用任何标准库的大数类型(比如 BigInteger)</strong><strong>直接将输入转换为整数来处理</strong></li></ol>

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
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);
	}
};
```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```