leetcode 71 Simplify Path ------ java

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

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

click to show corner cases.

Corner Cases:

  • 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".

这道题就是化简地址,有两个规则:  /./  代表不变

                  /../   代表返回上级目录

还有就是Corner Cases中的特殊情况都有说明

这道题刚开始写了好半天,其实主要原因是,题目中有一个没有说明,那就是带有转义字符的情况:如果目录名字中含有‘/’,如果考虑这种情况会复杂许多。但是这道题其实没有考虑这种情况,也就是说将\这个符号当成一般的字符处理。所以也就没什么难点,就是考虑清楚情况。

可以使用自动机求解。

public class Solution {
    public String simplifyPath(String path) {
        int len = path.length();
        if (len == 0)
            return path;
        Stack<Character> stack = new Stack<Character>();
        int i = 0;
        while (i < len) {
            if (path.charAt(i) == ‘/‘) {
                while( i<len && path.charAt(i) == ‘/‘)
                        i++;
                i--;
                if( i+1 == len)
                    i++;
                else if( path.charAt(i+1) == ‘.‘){
                    if( i+2 == len || path.charAt(i+2) == ‘/‘)
                        i+=2;
                    else if( path.charAt(i+2) == ‘.‘){
                        if( i+3 == len || path.charAt(i+3) == ‘/‘){
                            int j = 0;

                            while( j<stack.size() )
                                if(  stack.pop() == ‘/‘)
                                    break;
                                j++;

                            i+=3;
                        }else{
                            stack.push(‘/‘);
                            stack.push(‘.‘);
                            stack.push(‘.‘);
                            i+=3;
                            while (i < len) {
                                if (path.charAt(i) == ‘/‘)
                                    break;
                                else
                                    stack.push(path.charAt(i));
                                i++;
                            }
                        }
                    }else{
                        stack.push(‘/‘);
                        stack.push(‘.‘);
                        i+=2;
                        while (i < len) {
                            if (path.charAt(i) == ‘/‘)
                                break;
                            else
                                stack.push(path.charAt(i));
                            i++;
                        }

                    }

                }else{
                    i++;
                    stack.push(‘/‘);

                    while (i < len) {
                        if (path.charAt(i) == ‘/‘)
                            break;
                        else
                            stack.push(path.charAt(i));
                        i++;
                    }
                }
            } else {
                if( path.charAt(i) == ‘.‘){
                    if( i+1 == len || path.charAt(i+1) == ‘/‘)
                        i+=2;
                    else if( i+2 == len || (path.charAt(i+1) == ‘.‘ && path.charAt(i+2) == ‘/‘))
                        i+=3;
                }else{
                    stack.push(‘/‘);
                while (i < len) {
                    if (path.charAt(i) == ‘/‘)
                        break;
                    else
                        stack.push(path.charAt(i));
                    i++;
                }
                }

                stack.push(‘/‘);
                while (i < len) {
                    if (path.charAt(i) == ‘/‘)
                        break;
                    else
                        stack.push(path.charAt(i));
                    i++;
                }
            }

        }
        len = stack.size();
        if( len == 0)
            return new String("/");
        char[] result = new char[len];
        for (int j = len - 1; j >= 0; j--) {
            result[j] = stack.pop();
        }
        return String.valueOf(result);

    }
}
时间: 2024-12-21 15:46:52

leetcode 71 Simplify Path ------ java的相关文章

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

[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" Corner Cases: Did you consider the case where path = "/../"?In th

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

LeetCode开心刷题四十八天——71. Simplify Path

71. Simplify Path Medium 5101348FavoriteShare Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a do

71. Simplify Path做题报告

题目链接: 71. Simplify Path 题目大意: 简化路径,如果对于‘.'字符,表明为此目录,对于‘..'字符,表明为回到此目录上一节点(即:删除上一节点),我们现在需要删除多余的'/'字符和'.'字符 做题报告: (1)该题涉及的算法与数据结构与知识点 Java NIO中的Files类,正则表达式,栈 (2)自己的解答思路+代码+分析时间和空间复杂度 栈 class Solution { public String simplifyPath(String path) { String

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

[C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case w

【一天一道LeetCode】#71. Simplify Path

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", =

Leetcode 之Simplify Path @ python

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" 使用一个栈来解决问题.遇到'..'弹栈,遇到'.'不操作,其他情况下压栈. 代码一: class Solution: # @param path, a