139、简化路径

题目描述:
以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。

在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (…) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径

请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能以 / 结尾。此外,规范路径必须是表示绝对路径的最短字符串。

示例 1:

输入:"/home/"
输出:"/home"
解释:注意,最后一个目录名后面没有斜杠。
示例 2:

输入:"/…/"
输出:"/"
解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。
示例 3:

输入:"/home//foo/"
输出:"/home/foo"
解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
示例 4:

输入:"/a/./b/…/…/c/"
输出:"/c"
示例 5:

输入:"/a/…/…/b/…/c//.//"
输出:"/c"
示例 6:

输入:"/a//b////c/d//././/…"
输出:"/a/b/c"

用一个栈即可,但是速度慢的原因可能是字符串分隔和替换那里可能比较慢
代码:

class Solution {
    public String simplifyPath(String path) {
    path = path.replace("//", "/");
		String tems [] = path.split("/");
		Stack<String> stack = new Stack<>();
		for (int i = 0; i < tems.length; i++) {
			String temString  = tems[i];
			if(temString.length() != 0){
				if(tems[i].equals(".")){
					continue;
				}else if (temString.equals("..")) {
					if(!stack.isEmpty()){
						stack.pop();
					}
				}else {
					stack.push(temString);
				}
			}
		}
		String result = "";
		while (!stack.isEmpty()) {
			result = "/" + stack.pop() + result;
		}
		if(result.length() == 0){
			result = "/";
		}
        return result;
    }
}

考虑另外一种思路:不用切分,使用indexof获取每个片段的具体内容
实现代码:

class Solution {
    public String simplifyPath(String path) {
        int i = path.length() - 1, count = 0;
        StringBuilder builder = new StringBuilder(i);
        while (i > 0) {
            int j = path.lastIndexOf('/', i);
            if (i == j) i--;
            else {
                String x = path.substring(j + 1, i + 1);
                i = j - 1;
                if (".".equals(x)) ;
                else if ("..".equals(x)) count++;
                else if (count > 0) count--;
                else builder.insert(0, "/" + x);
            }
        }
        return builder.length() == 0 ? "/" : builder.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34446716/article/details/93979328
今日推荐