File tree 3 files changed +85
-0
lines changed
solution/0000-0099/0071.Simplify Path
3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,36 @@ public:
156
156
};
157
157
```
158
158
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
+
159
189
### ** Go**
160
190
161
191
``` go
Original file line number Diff line number Diff line change @@ -140,6 +140,36 @@ public:
140
140
};
141
141
```
142
142
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
+
143
173
### ** Go**
144
174
145
175
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments