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

Simplify Path

时间:2016-07-17 13:30:40      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

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

Example

"/home/", => "/home"

"/a/./b/../../c/", => "/c"

Challenge 
  • Did you consider the case where path = "/../"?

    In this case, you should return "/".

  • Another corner case is the path might contain multiple slashes ‘/‘together, such as "/home//foo/".

    In this case, you should ignore redundant slashes and return"/home/foo".

 1 public class Solution {
 2     /**
 3      * @param path the original path
 4      * @return the simplified path
 5      * 归下类的话,有四种字符串:
 6 1. "/":为目录分隔符,用来分隔两个目录。
 7 2. ".":当前目录
 8 3. "..":上层目录
 9 4. 其他字符串:目录名
10 
11 简化的核心是要找出所有的目录,并且如果遇到"..",需要删除上一个目录。
12      */
13         public static String simplifyPath(String path) {
14         if (path == null || path.length() == 0) return path;
15 
16         String[] subPath = path.split("/");
17         Stack<String> stack = new Stack<String>();
18         for (int i = 0; i < subPath.length; i++) {
19             String str = subPath[i];
20             if (!(str.equals(".") || str.isEmpty())) { // for . and empty string, we do nothing.
21                 if (str.equals("..")) { // we need to remove one upper directory
22                     if (stack.size() >= 1) {
23                         stack.pop();
24                     }
25                 } else {
26                     stack.push("/" + str);
27                 }
28             }
29         }
30         
31         StringBuilder sb = new StringBuilder();
32         while (!stack.isEmpty()) {
33             sb.insert(0, stack.pop());
34         }
35         return sb.length() == 0 ? "/" : sb.toString();
36     }
37 
38 }

Use StringBuilder

 1 public class Solution {
 2         public static String simplifyPath(String path) {
 3         if (path == null || path.length() == 0) return path;
 4 
 5         String[] subPath = path.split("/");
 6         StringBuilder sb = new StringBuilder("");
 7         for (int i = 0; i < subPath.length; i++) {
 8             String str = subPath[i];
 9             if (!(str.equals(".") || str.isEmpty())) { // for . and empty string, we do nothing.
10                 if (str.equals("..")) { // we need to remove one upper directory
11                     if (sb.length() > 1) {
12                         int k = sb.length() - 1;
13                         while (sb.length() >= 1 && sb.charAt(k) != /) {
14                             sb.deleteCharAt(k);
15                             k--;
16                         }
17                         sb.deleteCharAt(k); // "delete the last /"
18                     }
19                 } else {
20                     sb.append("/");
21                     sb.append(str);
22                 }
23             }
24         }
25         if (sb.length() == 0) {
26             return "/";
27         }
28         return sb.toString();
29     }
30 }

 

 

Simplify Path

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5677656.html

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