Skip to content

Commit 14b7bbf

Browse files
committed
feat: add solutions to lc problem: No.0043
No.0043.Multiply Strings
1 parent 430c54a commit 14b7bbf

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

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

+60
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,66 @@ function addString(s1: string, s2: string): string {
156156
}
157157
```
158158

159+
```ts
160+
function multiply(num1: string, num2: string): string {
161+
const res = ['0'];
162+
if (num1 === '0' || num2 === '0') {
163+
return res.join('');
164+
}
165+
166+
const n = num1.length;
167+
const m = num2.length;
168+
for (let i = 0; i < n; i++) {
169+
const num = Number(num1[n - 1 - i]);
170+
let sum = 0;
171+
for (let j = 0; j < m || sum != 0; j++) {
172+
sum +=
173+
(Number(num2[m - 1 - j]) || 0) * num +
174+
(Number(res[i + j]) || 0);
175+
res[i + j] = `${sum % 10}`;
176+
sum = Math.floor(sum / 10);
177+
}
178+
}
179+
180+
return res.reverse().join('');
181+
}
182+
```
183+
184+
### **Rust**
185+
186+
```rust
187+
impl Solution {
188+
pub fn multiply(num1: String, num2: String) -> String {
189+
if num1 == "0" || num2 == "0" {
190+
return String::from("0");
191+
}
192+
let mut res = vec![0];
193+
let num1 = num1.as_bytes();
194+
let num2 = num2.as_bytes();
195+
let n = num1.len();
196+
let m = num2.len();
197+
for i in 0..n {
198+
let num = num1[n - i - 1] - b'0';
199+
let mut sum = 0;
200+
let mut j = 0;
201+
while j < m || sum != 0 {
202+
if i + j == res.len() {
203+
res.push(0);
204+
}
205+
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
206+
res[i + j] = sum % 10;
207+
sum /= 10;
208+
j += 1;
209+
}
210+
}
211+
res.iter()
212+
.rev()
213+
.map(|num| num.to_string())
214+
.collect::<String>()
215+
}
216+
}
217+
```
218+
159219
### **...**
160220

161221
```

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

+60
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,66 @@ function addString(s1: string, s2: string): string {
141141
}
142142
```
143143

144+
```ts
145+
function multiply(num1: string, num2: string): string {
146+
const res = ['0'];
147+
if (num1 === '0' || num2 === '0') {
148+
return res.join('');
149+
}
150+
151+
const n = num1.length;
152+
const m = num2.length;
153+
for (let i = 0; i < n; i++) {
154+
const num = Number(num1[n - 1 - i]);
155+
let sum = 0;
156+
for (let j = 0; j < m || sum != 0; j++) {
157+
sum +=
158+
(Number(num2[m - 1 - j]) || 0) * num +
159+
(Number(res[i + j]) || 0);
160+
res[i + j] = `${sum % 10}`;
161+
sum = Math.floor(sum / 10);
162+
}
163+
}
164+
165+
return res.reverse().join('');
166+
}
167+
```
168+
169+
### **Rust**
170+
171+
```rust
172+
impl Solution {
173+
pub fn multiply(num1: String, num2: String) -> String {
174+
if num1 == "0" || num2 == "0" {
175+
return String::from("0");
176+
}
177+
let mut res = vec![0];
178+
let num1 = num1.as_bytes();
179+
let num2 = num2.as_bytes();
180+
let n = num1.len();
181+
let m = num2.len();
182+
for i in 0..n {
183+
let num = num1[n - i - 1] - b'0';
184+
let mut sum = 0;
185+
let mut j = 0;
186+
while j < m || sum != 0 {
187+
if i + j == res.len() {
188+
res.push(0);
189+
}
190+
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
191+
res[i + j] = sum % 10;
192+
sum /= 10;
193+
j += 1;
194+
}
195+
}
196+
res.iter()
197+
.rev()
198+
.map(|num| num.to_string())
199+
.collect::<String>()
200+
}
201+
}
202+
```
203+
144204
### **...**
145205

146206
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn multiply(num1: String, num2: String) -> String {
3+
if num1 == "0" || num2 == "0" {
4+
return String::from("0");
5+
}
6+
let mut res = vec![0];
7+
let num1 = num1.as_bytes();
8+
let num2 = num2.as_bytes();
9+
let n = num1.len();
10+
let m = num2.len();
11+
for i in 0..n {
12+
let num = num1[n - i - 1] - b'0';
13+
let mut sum = 0;
14+
let mut j = 0;
15+
while j < m || sum != 0 {
16+
if i + j == res.len() {
17+
res.push(0);
18+
}
19+
sum += num * (num2.get(m - j - 1).unwrap_or(&b'0') - b'0') + res[i + j];
20+
res[i + j] = sum % 10;
21+
sum /= 10;
22+
j += 1;
23+
}
24+
}
25+
res.iter()
26+
.rev()
27+
.map(|num| num.to_string())
28+
.collect::<String>()
29+
}
30+
}

0 commit comments

Comments
 (0)