Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 67fdde9

Browse files
committed
feat: add solutions to lc problem: No.0043
No.0043.Multiply Strings
1 parent 8f9321e commit 67fdde9

File tree

6 files changed

+203
-252
lines changed

6 files changed

+203
-252
lines changed

‎solution/0000-0099/0043.Multiply Strings/README.md‎

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:数学乘法模拟**
42+
43+
假设 `num1``num2` 的长度分别为 $m$ 和 $n,ドル则它们的乘积的长度最多为 $m + n$。
44+
45+
证明如下:
46+
47+
- 如果 `num1``num2` 都取最小值,那么它们的乘积为 ${10}^{m - 1} \times {10}^{n - 1} = {10}^{m + n - 2},ドル长度为 $m + n - 1$。
48+
- 如果 `num1``num2` 都取最大值,那么它们的乘积为 $({10}^m - 1) \times ({10}^n - 1) = {10}^{m + n} - {10}^m - {10}^n + 1,ドル长度为 $m + n$。
49+
50+
因此,我们可以申请一个长度为 $m + n$ 的数组,用于存储乘积的每一位。
51+
52+
从低位到高位,依次计算乘积的每一位,最后将数组转换为字符串即可。
53+
54+
注意判断最高位是否为 0ドル,ドル如果是,则去掉。
55+
56+
时间复杂度 $O(m \times n),ドル空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为 `num1``num2` 的长度。
57+
4158
<!-- tabs:start -->
4259

4360
### **Python3**
@@ -47,27 +64,20 @@
4764
```python
4865
class Solution:
4966
def multiply(self, num1: str, num2: str) -> str:
50-
def mul(b, i):
51-
j, t = m - 1, 0
52-
while j >= 0 or t:
53-
if j >= 0:
54-
a = int(num1[j])
55-
t += a * b
56-
res[i] += t % 10
57-
if res[i] >= 10:
58-
res[i] %= 10
59-
res[i + 1] += 1
60-
i, j = i + 1, j - 1
61-
t //= 10
62-
67+
if num1 == "0" or num2 == "0":
68+
return "0"
6369
m, n = len(num1), len(num2)
64-
res = [0] * (m + n)
65-
for i in range(n):
66-
b = int(num2[n - 1 - i])
67-
mul(b, i)
68-
while len(res) > 1 and res[-1] == 0:
69-
res.pop()
70-
return ''.join([str(v) for v in res[::-1]])
70+
arr = [0] * (m + n)
71+
for i in range(m - 1, -1, -1):
72+
a = int(num1[i])
73+
for j in range(n - 1, -1, -1):
74+
b = int(num2[j])
75+
arr[i + j + 1] += a * b
76+
for i in range(m + n - 1, 0, -1):
77+
arr[i - 1] += arr[i] // 10
78+
arr[i] %= 10
79+
i = 0 if arr[0] else 1
80+
return "".join(str(x) for x in arr[i:])
7181
```
7282

7383
### **Java**
@@ -77,35 +87,28 @@ class Solution:
7787
```java
7888
class Solution {
7989
public String multiply(String num1, String num2) {
90+
if ("0".equals(num1) || "0".equals(num2)) {
91+
return "0";
92+
}
8093
int m = num1.length(), n = num2.length();
81-
int[] res = new int[m + n];
82-
for (int i = 0; i < n; ++i) {
83-
int b = num2.charAt(n - 1 - i) - '0';
84-
mul(num1, b, i, res);
94+
int[] arr = new int[m + n];
95+
for (int i = m - 1; i >= 0; --i) {
96+
int a = num1.charAt(i) - '0';
97+
for (int j = n - 1; j >= 0; --j) {
98+
int b = num2.charAt(j) - '0';
99+
arr[i + j + 1] += a * b;
100+
}
85101
}
86-
StringBuilder ans =newStringBuilder();
87-
for (int v : res) {
88-
ans.append(v);
102+
for (int i = arr.length -1; i >0; --i) {
103+
arr[i -1] += arr[i] /10;
104+
arr[i] %=10;
89105
}
90-
while (ans.length() > 1 && ans.charAt(ans.length() - 1) == '0') {
91-
ans.deleteCharAt(ans.length() - 1);
92-
}
93-
return ans.reverse().toString();
94-
}
95-
96-
private void mul(String A, int b, int i, int[] res) {
97-
for (int j = A.length() - 1, t = 0; j >= 0 || t > 0; --j) {
98-
if (j >= 0) {
99-
int a = A.charAt(j) - '0';
100-
t += a * b;
101-
}
102-
res[i++] += t % 10;
103-
if (res[i - 1] >= 10) {
104-
res[i - 1] %= 10;
105-
++res[i];
106-
}
107-
t /= 10;
106+
int i = arr[0] == 0 ? 1 : 0;
107+
StringBuilder ans = new StringBuilder();
108+
for (; i < arr.length; ++i) {
109+
ans.append(arr[i]);
108110
}
111+
return ans.toString();
109112
}
110113
}
111114
```
@@ -116,32 +119,28 @@ class Solution {
116119
class Solution {
117120
public:
118121
string multiply(string num1, string num2) {
119-
int m = num1.size(), n = num2.size();
120-
vector<int> res(m + n);
121-
for (int i = 0; i < n; ++i) {
122-
int b = num2[n - 1 - i] - '0';
123-
mul(num1, b, i, res);
122+
if (num1 == "0" || num2 == "0") {
123+
return "0";
124124
}
125-
string ans = "";
126-
for (int v : res) ans.push_back(v + '0');
127-
while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
128-
reverse(ans.begin(), ans.end());
129-
return ans;
130-
}
131-
132-
void mul(string A, int b, int i, vector<int>& res) {
133-
for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) {
134-
if (j >= 0) {
135-
int a = A[j] - '0';
136-
t += a * b;
137-
}
138-
res[i++] += t % 10;
139-
if (res[i - 1] >= 10) {
140-
res[i - 1] %= 10;
141-
++res[i];
125+
int m = num1.size(), n = num2.size();
126+
vector<int> arr(m + n);
127+
for (int i = m - 1; i >= 0; --i) {
128+
int a = num1[i] - '0';
129+
for (int j = n - 1; j >= 0; --j) {
130+
int b = num2[j] - '0';
131+
arr[i + j + 1] += a * b;
142132
}
143-
t /= 10;
144133
}
134+
for (int i = arr.size() - 1; i; --i) {
135+
arr[i - 1] += arr[i] / 10;
136+
arr[i] %= 10;
137+
}
138+
int i = arr[0] ? 0 : 1;
139+
string ans;
140+
for (; i < arr.size(); ++i) {
141+
ans += '0' + arr[i];
142+
}
143+
return ans;
145144
}
146145
};
147146
```

‎solution/0000-0099/0043.Multiply Strings/README_EN.md‎

Lines changed: 68 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -34,63 +34,49 @@
3434
```python
3535
class Solution:
3636
def multiply(self, num1: str, num2: str) -> str:
37-
def mul(b, i):
38-
j, t = m - 1, 0
39-
while j >= 0 or t:
40-
if j >= 0:
41-
a = int(num1[j])
42-
t += a * b
43-
res[i] += t % 10
44-
if res[i] >= 10:
45-
res[i] %= 10
46-
res[i + 1] += 1
47-
i, j = i + 1, j - 1
48-
t //= 10
49-
37+
if num1 == "0" or num2 == "0":
38+
return "0"
5039
m, n = len(num1), len(num2)
51-
res = [0] * (m + n)
52-
for i in range(n):
53-
b = int(num2[n - 1 - i])
54-
mul(b, i)
55-
while len(res) > 1 and res[-1] == 0:
56-
res.pop()
57-
return ''.join([str(v) for v in res[::-1]])
40+
arr = [0] * (m + n)
41+
for i in range(m - 1, -1, -1):
42+
a = int(num1[i])
43+
for j in range(n - 1, -1, -1):
44+
b = int(num2[j])
45+
arr[i + j + 1] += a * b
46+
for i in range(m + n - 1, 0, -1):
47+
arr[i - 1] += arr[i] // 10
48+
arr[i] %= 10
49+
i = 0 if arr[0] else 1
50+
return "".join(str(x) for x in arr[i:])
5851
```
5952

6053
### **Java**
6154

6255
```java
6356
class Solution {
6457
public String multiply(String num1, String num2) {
65-
int m = num1.length(), n = num2.length();
66-
int[] res = new int[m + n];
67-
for (int i = 0; i < n; ++i) {
68-
int b = num2.charAt(n - 1 - i) - '0';
69-
mul(num1, b, i, res);
58+
if ("0".equals(num1) || "0".equals(num2)) {
59+
return "0";
7060
}
71-
StringBuilder ans = new StringBuilder();
72-
for (int v : res) {
73-
ans.append(v);
61+
int m = num1.length(), n = num2.length();
62+
int[] arr = new int[m + n];
63+
for (int i = m - 1; i >= 0; --i) {
64+
int a = num1.charAt(i) - '0';
65+
for (int j = n - 1; j >= 0; --j) {
66+
int b = num2.charAt(j) - '0';
67+
arr[i + j + 1] += a * b;
68+
}
7469
}
75-
while (ans.length() > 1 && ans.charAt(ans.length() - 1) == '0') {
76-
ans.deleteCharAt(ans.length() - 1);
70+
for (int i = arr.length - 1; i > 0; --i) {
71+
arr[i - 1] += arr[i] / 10;
72+
arr[i] %= 10;
7773
}
78-
return ans.reverse().toString();
79-
}
80-
81-
private void mul(String A, int b, int i, int[] res) {
82-
for (int j = A.length() - 1, t = 0; j >= 0 || t > 0; --j) {
83-
if (j >= 0) {
84-
int a = A.charAt(j) - '0';
85-
t += a * b;
86-
}
87-
res[i++] += t % 10;
88-
if (res[i - 1] >= 10) {
89-
res[i - 1] %= 10;
90-
++res[i];
91-
}
92-
t /= 10;
74+
int i = arr[0] == 0 ? 1 : 0;
75+
StringBuilder ans = new StringBuilder();
76+
for (; i < arr.length; ++i) {
77+
ans.append(arr[i]);
9378
}
79+
return ans.toString();
9480
}
9581
}
9682
```
@@ -101,32 +87,28 @@ class Solution {
10187
class Solution {
10288
public:
10389
string multiply(string num1, string num2) {
104-
int m = num1.size(), n = num2.size();
105-
vector<int> res(m + n);
106-
for (int i = 0; i < n; ++i) {
107-
int b = num2[n - 1 - i] - '0';
108-
mul(num1, b, i, res);
90+
if (num1 == "0" || num2 == "0") {
91+
return "0";
10992
}
110-
string ans = "";
111-
for (int v : res) ans.push_back(v + '0');
112-
while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
113-
reverse(ans.begin(), ans.end());
114-
return ans;
115-
}
116-
117-
void mul(string A, int b, int i, vector<int>& res) {
118-
for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) {
119-
if (j >= 0) {
120-
int a = A[j] - '0';
121-
t += a * b;
122-
}
123-
res[i++] += t % 10;
124-
if (res[i - 1] >= 10) {
125-
res[i - 1] %= 10;
126-
++res[i];
93+
int m = num1.size(), n = num2.size();
94+
vector<int> arr(m + n);
95+
for (int i = m - 1; i >= 0; --i) {
96+
int a = num1[i] - '0';
97+
for (int j = n - 1; j >= 0; --j) {
98+
int b = num2[j] - '0';
99+
arr[i + j + 1] += a * b;
127100
}
128-
t /= 10;
129101
}
102+
for (int i = arr.size() - 1; i; --i) {
103+
arr[i - 1] += arr[i] / 10;
104+
arr[i] %= 10;
105+
}
106+
int i = arr[0] ? 0 : 1;
107+
string ans;
108+
for (; i < arr.size(); ++i) {
109+
ans += '0' + arr[i];
110+
}
111+
return ans;
130112
}
131113
};
132114
```
@@ -135,35 +117,29 @@ public:
135117
136118
```go
137119
func multiply(num1 string, num2 string) string {
120+
if num1 == "0" || num2 == "0" {
121+
return "0"
122+
}
138123
m, n := len(num1), len(num2)
139-
res := make([]int, m+n)
140-
mul := func(b, i int) {
141-
for j, t := m-1, 0; j >= 0 || t > 0; i, j = i+1, j-1 {
142-
if j >= 0 {
143-
a := int(num1[j] - '0')
144-
t += a * b
145-
}
146-
res[i] += t % 10
147-
if res[i] >= 10 {
148-
res[i] %= 10
149-
res[i+1]++
150-
}
151-
t /= 10
124+
arr := make([]int, m+n)
125+
for i := m - 1; i >= 0; i-- {
126+
a := int(num1[i] - '0')
127+
for j := n - 1; j >= 0; j-- {
128+
b := int(num2[j] - '0')
129+
arr[i+j+1] += a * b
152130
}
153131
}
154-
for i := 0; i < n; i++ {
155-
b := num2[n-1-i] - '0'
156-
mul(int(b), i)
157-
}
158-
var ans []byte
159-
for _, v := range res {
160-
ans = append(ans, byte(v+'0'))
132+
for i := len(arr) - 1; i > 0; i-- {
133+
arr[i-1] += arr[i] / 10
134+
arr[i] %= 10
161135
}
162-
for len(ans) > 1 && ans[len(ans)-1] == '0' {
163-
ans = ans[:len(ans)-1]
136+
i := 0
137+
if arr[0] == 0 {
138+
i = 1
164139
}
165-
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
166-
ans[i], ans[j] = ans[j], ans[i]
140+
ans := []byte{}
141+
for ; i < len(arr); i++ {
142+
ans = append(ans, byte('0'+arr[i]))
167143
}
168144
return string(ans)
169145
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /