-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0043-multiply-strings.rs
More file actions
28 lines (25 loc) · 899 Bytes
/
0043-multiply-strings.rs
File metadata and controls
28 lines (25 loc) · 899 Bytes
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
impl Solution {
pub fn multiply(num1: String, num2: String) -> String {
if num1 == "0" || num2 == "0" {
return String::from('0');
}
let n1 = num1.chars().rev().collect::<String>();
let n2 = num2.chars().rev().collect::<String>();
let mut res = vec![0; n1.len() + n2.len()];
for (i1, c1) in n1.chars().enumerate() {
for (i2, c2) in n2.chars().enumerate() {
let digit = (c1.to_digit(10).unwrap_or(0) * c2.to_digit(10).unwrap_or(0));
res[i1 + i2] += digit;
res[i1 + i2 + 1] += res[i1 + i2] / 10;
res[i1 + i2] %= 10;
}
}
while let Some(&0) = res.last() {
res.pop();
}
res.into_iter()
.rev()
.map(|digit| char::from_digit(digit as u32, 10).unwrap())
.collect()
}
}