-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplify_path.rs
More file actions
29 lines (28 loc) · 790 Bytes
/
Copy pathsimplify_path.rs
File metadata and controls
29 lines (28 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct Solution;
impl Solution {
pub fn simplify_path(path: String) -> String {
let dirs = path.split("/").filter(|&s| s != "").collect::<Vec<_>>();
let mut ans = Vec::new();
for &x in dirs.iter() {
match x {
".." => {
ans.pop();
}
"." => {
continue;
}
_ => {
ans.push(x);
}
}
}
let mut ans = ans.join("/");
ans.insert(0, '/');
ans
}
}
fn main() {
Solution::simplify_path("/home/user/Documents/../Pictures".into());
println!("{:?}", Solution::simplify_path("/../".into()));
Solution::simplify_path("/.../a/../b/c/../d/./".into());
}