71. Simplify Path

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

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

题目含义:简化unix路径

 1     public String simplifyPath(String path) {
 2         Stack<String > stack = new Stack<>();
 3         List<String> skip = Arrays.asList("..",".","");
 4         for (String str:path.split("/"))
 5         {
 6             if (str.equals("..") && !stack.isEmpty()) stack.pop();
 7             else if (!skip.contains(str)) stack.push(str);
 8         }
 9         String result="";
10         for (String item:stack)
11         {
12             result +=  "/" + item;
13         }
14         return result.isEmpty()?"/":result;     
15     }

 

posted @ 2017-10-18 10:08  daniel456  阅读(97)  评论(0编辑  收藏  举报