zoukankan      html  css  js  c++  java
  • leetcode — simplify-path

    import java.util.Stack;
    
    /**
     *
     * Source : https://oj.leetcode.com/problems/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 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".
     *
     */
    public class SimplifyPath {
    
        /**
         * 简化unix形式的文件路径
         * 考虑几种特殊情况
         * 有多个斜杠
         * 只剩下根路径的情况
         * 路径中文件夹名称包含.
         *
         * @param path
         * @return
         */
        public String simplify (String path) {
            Stack<String> stack = new Stack<String>();
            stack.push("/");
            String[] paths = path.split("/");
            for (int i = 0; i < paths.length; i++) {
                String cur = paths[i];
                if (cur.equals("/") || cur.equals(".") || cur.equals("")) {
                    continue;
                }
                if (cur.equals("..")) {
                    if (stack.size() > 1) {
                        stack.pop();
                    }
                } else {
                    stack.push(cur);
                }
            }
            if (stack.size() == 0) {
                return "/";
            }
            String result = "";
            for (int i = 0; i < stack.size(); i++) {
                result += stack.get(i);
                if (stack.get(i).equals("/")) {
                    continue;
                }
                result += "/";
            }
            if (result.length() == 1) {
                return result;
            }
            return result.substring(0, result.length() - 1);
        }
    
        public static void main(String[] args) {
            SimplifyPath simplifyPath = new SimplifyPath();
            System.out.println(simplifyPath.simplify("/home/"));
            System.out.println(simplifyPath.simplify("/a/./b/../../c/"));
            System.out.println(simplifyPath.simplify("/../"));
            System.out.println(simplifyPath.simplify("/../a/b"));
            System.out.println(simplifyPath.simplify("/home//foo/"));
            System.out.println(simplifyPath.simplify("/home///foo/"));
            System.out.println(simplifyPath.simplify("/home/foo.bar/"));
            System.out.println(simplifyPath.simplify("/home/.bar/"));
        }
    }
    
  • 相关阅读:
    C#高性能大容量SOCKET并发(转)
    使用 NuGet 更新套件時將 jQuery 升級到 2.0.2 應該如何降級
    《TD式创新”祸国殃民》
    技术负责人的三种角色
    WaitForSingleObject和CEvent用法
    C++和C#转换
    C#与C/C++的交互
    Introducing .NET Core
    NHibernate的调试技巧和Log4Net配置
    Using Windows Server 2012 Backup for Hyper-V Virtual Machines. Error 80780176
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7714381.html
Copyright © 2011-2022 走看看