feat: 71_simplify_path
This commit is contained in:
parent
a908102fc1
commit
cb60387170
7
71_simplify_path/Cargo.lock
generated
Normal file
7
71_simplify_path/Cargo.lock
generated
Normal 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"
|
6
71_simplify_path/Cargo.toml
Normal file
6
71_simplify_path/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "simplify_path"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
32
71_simplify_path/src/lib.rs
Normal file
32
71_simplify_path/src/lib.rs
Normal 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user