-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0043_multiply_strings.rs
More file actions
57 lines (48 loc) · 1.37 KB
/
s0043_multiply_strings.rs
File metadata and controls
57 lines (48 loc) · 1.37 KB
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
#![allow(unused)]
pub struct Solution {}
impl Solution {
// Time O(N * M) space O(N + M)
pub fn multiply(num1: String, num2: String) -> String {
if num1.len() < 1 || num2.len() < 1 {
return "".to_string();
}
let mut ans = vec![0; num1.len() + num2.len()];
let (mut i, mut j) = (num1.len(), num2.len());
for b in num2.chars().rev() {
for a in num1.chars().rev() {
let prod = b.to_digit(10).unwrap() * a.to_digit(10).unwrap();
let sum = prod + ans[i + j - 1];
ans[i + j - 2] = ans[i + j - 2] + sum / 10;
ans[i + j - 1] = sum % 10;
j -= 1;
}
j = num2.len();
i -= 1;
}
let ans_str = ans
.into_iter()
.map(|d| std::char::from_digit(d, 10).unwrap())
.skip_while(|c| *c == '0')
.collect();
if ans_str == "".to_string() {
"0".to_string()
} else {
ans_str
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_43() {
assert_eq!(
Solution::multiply("2".to_string(), "3".to_string()),
"6".to_string()
);
assert_eq!(
Solution::multiply("123".to_string(), "456".to_string()),
"56088".to_string()
);
}
}