20240517 Finished

This commit is contained in:
jackfiled 2024-05-17 08:24:53 +08:00
parent 098e5ca766
commit e1af301e2a
2 changed files with 55 additions and 1 deletions

View File

@ -134,3 +134,4 @@ mod p228_summary_ranges;
mod p56_merge_intervals; mod p56_merge_intervals;
mod p57_insert_interval; mod p57_insert_interval;
mod p452_minimum_number_of_arrows_to_burst_balloons; mod p452_minimum_number_of_arrows_to_burst_balloons;
mod p71_simplify_path;

View File

@ -0,0 +1,53 @@
/**
* [71] Simplify Path
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn simplify_path(path: String) -> String {
let names = path.split('/');
let mut path = vec![];
for name in names {
if name.len() == 0 {
continue;
}
if name == "." {
continue;
} else if name == ".." {
path.pop();
} else {
path.push(name)
}
}
let mut result = String::new();
for name in path {
result.push_str("/");
result.push_str(name);
}
if result.len() == 0 {
"/".to_owned()
} else {
result
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_71() {
assert_eq!("/home".to_owned(), Solution::simplify_path("/home//".to_owned()));
}
}