码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 71: Simplify Path

时间:2017-11-10 12:42:37      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:count   solution   for   absolute   ==   span   pen   ret   log   

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

标签:count   solution   for   absolute   ==   span   pen   ret   log   

原文地址:http://www.cnblogs.com/liangmou/p/7813782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!