feat: 71_simplify_path

This commit is contained in:
SquidSpirit 2025-09-27 14:13:08 +08:00
parent a908102fc1
commit cb60387170
3 changed files with 45 additions and 0 deletions

7
71_simplify_path/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -0,0 +1,6 @@
[package]
name = "simplify_path"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -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
}
}