Leetcode 71: Simplify Path

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 71: Simplify Path相关的知识,希望对你有一定的参考价值。

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

 

 1 public class Solution {
 2     public string SimplifyPath(string path) {
 3         if (path.Length == 0) return path;
 4         
 5         var pathes = path.Split(/);
 6         var list = new List<string>();
 7         var sb = new StringBuilder();
 8         
 9         foreach (var p in pathes)
10         {
11             if (p == "..")
12             {
13                 if (list.Count > 0)
14                 {
15                     list.RemoveAt(list.Count - 1);
16                 }
17             }
18             else if (p != "" && p != ".")
19             {
20                 list.Add(p);
21             }
22         }
23                 
24         sb.Append(/);
25         for (int i = 0; i < list.Count; i++)
26         {
27             sb.Append(list[i]);
28             
29             if (i != list.Count - 1)
30             {         
31                 sb.Append(/);       
32             }
33         }
34         
35         return sb.ToString();
36     }
37 }

 



以上是关于Leetcode 71: Simplify Path的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-71-Simplify Path

Leetcode 71: Simplify Path

leetcode71. Simplify Path

[LeetCode] 71. Simplify Path 简化路径

leetcode 71 Simplify Path ------ java

[LeetCode] 71. Simplify Path