20240517 Finished
This commit is contained in:
parent
098e5ca766
commit
e1af301e2a
|
@ -133,4 +133,5 @@ mod p128_longest_consecutive_sequence;
|
|||
mod p228_summary_ranges;
|
||||
mod p56_merge_intervals;
|
||||
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;
|
53
src/problem/p71_simplify_path.rs
Normal file
53
src/problem/p71_simplify_path.rs
Normal 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()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user