From 0bd5fbb55bd20559da3db4246601cb65940720c6 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 8 May 2024 12:42:07 +0800 Subject: [PATCH] leetcode: Add 0071 --- src/leetcode/0071.simplify-path/Cargo.toml | 7 ++ src/leetcode/0071.simplify-path/index.md | 4 + src/leetcode/0071.simplify-path/src/main.rs | 85 +++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 src/leetcode/0071.simplify-path/Cargo.toml create mode 100644 src/leetcode/0071.simplify-path/index.md create mode 100644 src/leetcode/0071.simplify-path/src/main.rs diff --git a/src/leetcode/0071.simplify-path/Cargo.toml b/src/leetcode/0071.simplify-path/Cargo.toml new file mode 100644 index 00000000..f505ec9f --- /dev/null +++ b/src/leetcode/0071.simplify-path/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0071-simplify-path" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0071.simplify-path/index.md b/src/leetcode/0071.simplify-path/index.md new file mode 100644 index 00000000..77a618ba --- /dev/null +++ b/src/leetcode/0071.simplify-path/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0071.simplify-path/src/main.rs b/src/leetcode/0071.simplify-path/src/main.rs new file mode 100644 index 00000000..2289d3a3 --- /dev/null +++ b/src/leetcode/0071.simplify-path/src/main.rs @@ -0,0 +1,85 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +// Stack +pub fn simplify_path1(path: String) -> String { + let mut stack: Vec = Vec::new(); + for part in path.split('/') { + match part { + // 忽略空白 + "" => (), + // 忽略当前目录 + "." => (), + // 返回上个目录 + ".." => { + let _ = stack.pop(); + } + part => stack.push(part.to_owned()), + } + } + // 根目录 + let mut result = "/".to_owned(); + result.push_str(&stack.join("/")); + result +} + +// Stack +// 优化字符串操作 +pub fn simplify_path2(path: String) -> String { + let mut stack: Vec<&str> = Vec::with_capacity(path.len()); + for part in path.split('/') { + match part { + // 忽略空白 + "" => (), + // 忽略当前目录 + "." => (), + // 返回上个目录 + ".." => { + let _ = stack.pop(); + } + part => stack.push(part), + } + } + // 目录以 "/" 开头 + format!("/{}", stack.join("/")) +} + +pub type SolutionFn = fn(String) -> String; + +fn check_solution(func: SolutionFn) { + let path = "/home/".to_owned(); + assert_eq!(&func(path), "/home"); + + let path = "/home//foo/".to_owned(); + assert_eq!(&func(path), "/home/foo"); + + let path = "/home/user/Documents/../Pictures".to_owned(); + assert_eq!(&func(path), "/home/user/Pictures"); + + let path = "/../".to_owned(); + assert_eq!(&func(path), "/"); + + let path = "/.../a/../b/c/../d/./".to_owned(); + assert_eq!(&func(path), "/.../b/d"); +} + +fn main() { + check_solution(simplify_path1); + check_solution(simplify_path2); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, simplify_path1, simplify_path2}; + + #[test] + fn test_simplify_path1() { + check_solution(simplify_path1); + } + + #[test] + fn test_simplify_path2() { + check_solution(simplify_path2); + } +}