File tree Expand file tree Collapse file tree 3 files changed +198
-0
lines changed
solution/0000-0099/0071.Simplify Path Expand file tree Collapse file tree 3 files changed +198
-0
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,92 @@ func simplifyPath(path string) string {
140
140
}
141
141
```
142
142
143
+ ### ** C#**
144
+
145
+ ``` cs
146
+ using System .Collections .Generic ;
147
+ using System .Linq ;
148
+ using System .Text ;
149
+
150
+ public class Solution {
151
+ public string SimplifyPath (string path ) {
152
+ var stack = new Stack <string >();
153
+ var sb = new StringBuilder ();
154
+ foreach (var ch in ((IEnumerable <char >)path ).Concat (Enumerable .Repeat ('/' , 1 )))
155
+ {
156
+ if (ch == '/' )
157
+ {
158
+ if (sb .Length > 0 )
159
+ {
160
+ var folder = sb .ToString ();
161
+ sb .Clear ();
162
+ switch (folder )
163
+ {
164
+ case " ." :
165
+ break ;
166
+ case " .." :
167
+ if (stack .Any ())
168
+ {
169
+ stack .Pop ();
170
+ }
171
+ break ;
172
+ default :
173
+ stack .Push (folder );
174
+ break ;
175
+ }
176
+ }
177
+ }
178
+ else
179
+ {
180
+ sb .Append (ch );
181
+ }
182
+ }
183
+
184
+ if (stack .Count == 0 )
185
+ {
186
+ sb .Append ('/' );
187
+ }
188
+ foreach (var folder in ((IEnumerable <string >)stack .ToList ()).Reverse ())
189
+ {
190
+ sb .Append ('/' );
191
+ sb .Append (folder );
192
+ }
193
+ return sb .ToString ();
194
+ }
195
+ }
196
+ ```
197
+
198
+ ### ** TypeScript**
199
+
200
+ ``` ts
201
+ function simplifyPath(path : string ): string {
202
+ // 添加辅助斜线
203
+ path += " /" ;
204
+
205
+ const stack = [];
206
+ let str = " " ;
207
+ for (let i = 1 ; i < path .length ; i ++ ) {
208
+ const c = path [i ];
209
+ if (c === " /" ) {
210
+ if (str !== " " && str !== " ." ) {
211
+ if (str === " .." ) {
212
+ if (stack .length !== 0 ) {
213
+ stack .pop ();
214
+ }
215
+ } else {
216
+ stack .push (str );
217
+ }
218
+ }
219
+ str = " " ;
220
+ } else {
221
+ str += c ;
222
+ }
223
+ }
224
+
225
+ return " /" + stack .join (" /" );
226
+ }
227
+ ```
228
+
143
229
### ** ...**
144
230
145
231
```
Original file line number Diff line number Diff line change @@ -131,6 +131,92 @@ func simplifyPath(path string) string {
131
131
}
132
132
```
133
133
134
+ ### ** C#**
135
+
136
+ ``` cs
137
+ using System .Collections .Generic ;
138
+ using System .Linq ;
139
+ using System .Text ;
140
+
141
+ public class Solution {
142
+ public string SimplifyPath (string path ) {
143
+ var stack = new Stack <string >();
144
+ var sb = new StringBuilder ();
145
+ foreach (var ch in ((IEnumerable <char >)path ).Concat (Enumerable .Repeat ('/' , 1 )))
146
+ {
147
+ if (ch == '/' )
148
+ {
149
+ if (sb .Length > 0 )
150
+ {
151
+ var folder = sb .ToString ();
152
+ sb .Clear ();
153
+ switch (folder )
154
+ {
155
+ case " ." :
156
+ break ;
157
+ case " .." :
158
+ if (stack .Any ())
159
+ {
160
+ stack .Pop ();
161
+ }
162
+ break ;
163
+ default :
164
+ stack .Push (folder );
165
+ break ;
166
+ }
167
+ }
168
+ }
169
+ else
170
+ {
171
+ sb .Append (ch );
172
+ }
173
+ }
174
+
175
+ if (stack .Count == 0 )
176
+ {
177
+ sb .Append ('/' );
178
+ }
179
+ foreach (var folder in ((IEnumerable <string >)stack .ToList ()).Reverse ())
180
+ {
181
+ sb .Append ('/' );
182
+ sb .Append (folder );
183
+ }
184
+ return sb .ToString ();
185
+ }
186
+ }
187
+ ```
188
+
189
+ ### ** TypeScript**
190
+
191
+ ``` ts
192
+ function simplifyPath(path : string ): string {
193
+ // 添加辅助斜线
194
+ path += " /" ;
195
+
196
+ const stack = [];
197
+ let str = " " ;
198
+ for (let i = 1 ; i < path .length ; i ++ ) {
199
+ const c = path [i ];
200
+ if (c === " /" ) {
201
+ if (str !== " " && str !== " ." ) {
202
+ if (str === " .." ) {
203
+ if (stack .length !== 0 ) {
204
+ stack .pop ();
205
+ }
206
+ } else {
207
+ stack .push (str );
208
+ }
209
+ }
210
+ str = " " ;
211
+ } else {
212
+ str += c ;
213
+ }
214
+ }
215
+
216
+ return " /" + stack .join (" /" );
217
+ }
218
+ ```
219
+
134
220
### ** ...**
135
221
136
222
```
Original file line number Diff line number Diff line change
1
+ function simplifyPath ( path : string ) : string {
2
+ // 添加辅助斜线
3
+ path += "/" ;
4
+
5
+ const stack = [ ] ;
6
+ let str = "" ;
7
+ for ( let i = 1 ; i < path . length ; i ++ ) {
8
+ const c = path [ i ] ;
9
+ if ( c === "/" ) {
10
+ if ( str !== "" && str !== "." ) {
11
+ if ( str === ".." ) {
12
+ if ( stack . length !== 0 ) {
13
+ stack . pop ( ) ;
14
+ }
15
+ } else {
16
+ stack . push ( str ) ;
17
+ }
18
+ }
19
+ str = "" ;
20
+ } else {
21
+ str += c ;
22
+ }
23
+ }
24
+
25
+ return "/" + stack . join ( "/" ) ;
26
+ }
You can’t perform that action at this time.
0 commit comments