Skip to content

Commit

Permalink
leetcode: Add 0071
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 8, 2024
1 parent 28b8b8d commit 0bd5fbb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0071.simplify-path/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0071-simplify-path"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0071.simplify-path/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
85 changes: 85 additions & 0 deletions src/leetcode/0071.simplify-path/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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<String> = 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);
}
}

0 comments on commit 0bd5fbb

Please sign in to comment.