-
Notifications
You must be signed in to change notification settings - Fork 112
/
071-SimplifyPath.cs
37 lines (31 loc) · 1.08 KB
/
071-SimplifyPath.cs
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
//-----------------------------------------------------------------------------
// Runtime: 132ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
using System.Text;
namespace LeetCode
{
public class _071_SimplifyPath
{
public string SimplifyPath(string path)
{
if (string.IsNullOrEmpty(path)) { return "/"; }
var folders = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var folder = string.Empty;
var builder = new StringBuilder();
var ignore = 0;
for (int i = folders.Length - 1; i >= 0; i--)
{
folder = folders[i];
if (folder.Equals(".")) { continue; }
if (folder.Equals("..")) { ignore++; continue; }
if (ignore > 0) { ignore--; continue; }
builder.Insert(0, folder);
builder.Insert(0, "/");
}
return builder.Length > 0 ? builder.ToString() : "/";
}
}
}