From e1af301e2a19dea5e6dd8b135a6c9cf224842459 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 17 May 2024 08:24:53 +0800 Subject: [PATCH] 20240517 Finished --- src/problem/mod.rs | 3 +- src/problem/p71_simplify_path.rs | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/problem/p71_simplify_path.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 8cd0bc5..fc9ed9a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -133,4 +133,5 @@ mod p128_longest_consecutive_sequence; mod p228_summary_ranges; mod p56_merge_intervals; mod p57_insert_interval; -mod p452_minimum_number_of_arrows_to_burst_balloons; \ No newline at end of file +mod p452_minimum_number_of_arrows_to_burst_balloons; +mod p71_simplify_path; \ No newline at end of file diff --git a/src/problem/p71_simplify_path.rs b/src/problem/p71_simplify_path.rs new file mode 100644 index 0000000..9c06c34 --- /dev/null +++ b/src/problem/p71_simplify_path.rs @@ -0,0 +1,53 @@ +/** + * [71] Simplify Path + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn simplify_path(path: String) -> String { + let names = path.split('/'); + let mut path = vec![]; + + for name in names { + if name.len() == 0 { + continue; + } + + if name == "." { + continue; + } else if name == ".." { + path.pop(); + } else { + path.push(name) + } + } + + let mut result = String::new(); + + for name in path { + result.push_str("/"); + result.push_str(name); + } + + if result.len() == 0 { + "/".to_owned() + } else { + result + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_71() { + assert_eq!("/home".to_owned(), Solution::simplify_path("/home//".to_owned())); + } +}