From e3de039c39342db5df8760a517b475e8bb65eca9 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 28 Oct 2022 17:23:37 -0700 Subject: [PATCH] accepted bigint --- multiply_strings/src/main.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/multiply_strings/src/main.rs b/multiply_strings/src/main.rs index 57356ca..d67a7b3 100644 --- a/multiply_strings/src/main.rs +++ b/multiply_strings/src/main.rs @@ -1,7 +1,10 @@ pub fn multiply(num1: String, num2: String) -> String { // - let mut out: Vec = Vec::from_iter((0..(num1.len() + num2.len())).into_iter().map(|_| 0)); + let mut out: Vec = (0..(num1.len() + num2.len())) + .into_iter() + .map(|_| 0) + .collect(); let l1 = num1.len(); let l2 = num2.len(); @@ -11,7 +14,7 @@ pub fn multiply(num1: String, num2: String) -> String { for (i2, d2) in num2.chars().rev().enumerate() { let mut mcarry = 0; - let mut tmp = Vec::from_iter((0..i2).into_iter().map(|_| 0)); + let mut tmp: Vec = (0..i2).into_iter().map(|_| 0).collect(); let d2 = d2.to_digit(10).unwrap(); for d1 in num1.chars().rev() { let d1 = d1.to_digit(10).unwrap(); @@ -25,16 +28,17 @@ pub fn multiply(num1: String, num2: String) -> String { } merge(&mut out, &tmp); } - let out = if let Some(last) = out.last() { + while let Some(last) = out.last() { if *last == 0 { let len = out.len() - 1; - &out[0..len] + out = out[0..len].to_vec(); } else { - &out[0..] + break; } - } else { - &out[0..] - }; + } + if out.is_empty() { + out.push(0); + } out.iter().rev().map(|d| d.to_string()).collect() } @@ -64,4 +68,6 @@ fn main() { .to_string(), "4000000000000000000000000000000000000000000000000000000000000000".to_string() )); + + dbg!(multiply("0".to_string(), "8".to_string())); }