Skip to content

Commit e426ede

Browse files
author
Xu
authored
feat: add rust solution to lc problem: No.0071 (#1303)
1 parent 75e26bb commit e426ede

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

solution/0000-0099/0071.Simplify Path/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ public:
156156
};
157157
```
158158
159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
#[allow(dead_code)]
164+
pub fn simplify_path(path: String) -> String {
165+
let mut s: Vec<&str> = Vec::new();
166+
167+
// Split the path
168+
let p_vec = path.split("/").collect::<Vec<&str>>();
169+
170+
// Traverse the path vector
171+
for p in p_vec {
172+
match p {
173+
// Do nothing for "" or "."
174+
"" | "." => continue,
175+
".." => {
176+
if !s.is_empty() {
177+
s.pop();
178+
}
179+
}
180+
_ => s.push(p),
181+
}
182+
}
183+
184+
"/".to_string() + &s.join("/")
185+
}
186+
}
187+
```
188+
159189
### **Go**
160190

161191
```go

solution/0000-0099/0071.Simplify Path/README_EN.md

+30
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ public:
140140
};
141141
```
142142
143+
### **Rust**
144+
145+
```rust
146+
impl Solution {
147+
#[allow(dead_code)]
148+
pub fn simplify_path(path: String) -> String {
149+
let mut s: Vec<&str> = Vec::new();
150+
151+
// Split the path
152+
let p_vec = path.split("/").collect::<Vec<&str>>();
153+
154+
// Traverse the path vector
155+
for p in p_vec {
156+
match p {
157+
// Do nothing for "" or "."
158+
"" | "." => continue,
159+
".." => {
160+
if !s.is_empty() {
161+
s.pop();
162+
}
163+
}
164+
_ => s.push(p),
165+
}
166+
}
167+
168+
"/".to_string() + &s.join("/")
169+
}
170+
}
171+
```
172+
143173
### **Go**
144174

145175
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn simplify_path(path: String) -> String {
4+
let mut s: Vec<&str> = Vec::new();
5+
6+
// Split the path
7+
let p_vec = path.split("/").collect::<Vec<&str>>();
8+
9+
// Traverse the path vector
10+
for p in p_vec {
11+
match p {
12+
// Do nothing for "" or "."
13+
"" | "." => continue,
14+
".." => {
15+
if !s.is_empty() {
16+
s.pop();
17+
}
18+
}
19+
_ => s.push(p),
20+
}
21+
}
22+
23+
"/".to_string() + &s.join("/")
24+
}
25+
}

0 commit comments

Comments
 (0)