-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path71.simplify-path.cs
More file actions
38 lines (31 loc) · 825 Bytes
/
71.simplify-path.cs
File metadata and controls
38 lines (31 loc) · 825 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
30
31
32
33
34
35
36
37
/*
* @lc app=leetcode id=71 lang=csharp
*
* [71] Simplify Path
*/
// @lc code=start
public class Solution {
public string SimplifyPath(string path) {
var parts = path.Split('/');
var stack = new Stack<string>();
foreach(var part in parts){
if(part == ".."){
if(stack.Any()){
stack.Pop();
}
continue;
}
if(part == "." || string.IsNullOrEmpty(part)){
continue;
}
stack.Push(part);
}
var builder = new StringBuilder();
foreach(var part in stack.Reverse()){
builder.Append("/");
builder.Append(part);
}
return builder.Length == 0 ? "/" : builder.ToString();
}
}
// @lc code=end