From cb6038717041abdbac4c63cef51ee699dfcb62e2 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Sat, 27 Sep 2025 14:13:08 +0800 Subject: [PATCH] feat: 71_simplify_path --- 71_simplify_path/Cargo.lock | 7 +++++++ 71_simplify_path/Cargo.toml | 6 ++++++ 71_simplify_path/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 71_simplify_path/Cargo.lock create mode 100644 71_simplify_path/Cargo.toml create mode 100644 71_simplify_path/src/lib.rs diff --git a/71_simplify_path/Cargo.lock b/71_simplify_path/Cargo.lock new file mode 100644 index 0000000..f15afd8 --- /dev/null +++ b/71_simplify_path/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "simplify_path" +version = "0.1.0" diff --git a/71_simplify_path/Cargo.toml b/71_simplify_path/Cargo.toml new file mode 100644 index 0000000..1433535 --- /dev/null +++ b/71_simplify_path/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "simplify_path" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/71_simplify_path/src/lib.rs b/71_simplify_path/src/lib.rs new file mode 100644 index 0000000..cf5f897 --- /dev/null +++ b/71_simplify_path/src/lib.rs @@ -0,0 +1,32 @@ +pub struct Solution; + +impl Solution { + pub fn simplify_path(path: String) -> String { + let mut stack: Vec<&str> = Vec::new(); + let parts = path.split('/'); + + for part in parts { + match part { + "" | "." => {} + ".." => { + stack.pop(); + } + _ => { + stack.push(part); + } + } + } + + let mut result = String::new(); + for part in stack { + result.push('/'); + result.push_str(part); + } + + if result.len() == 0 { + result.push('/'); + } + + result + } +}