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/"));
        }
    }
    
  • 相关阅读:
    Leetcode-921 Minimum Add to Make Parentheses Valid(使括号有效的最少添加)
    Leetcode-922 Sort Array By Parity II(按奇偶排序数组 II)
    Leetcode-827 Making A Large Island(最大人工岛)
    Leetcode-766 Toeplitz Matrix(托普利茨矩阵)
    Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7714381.html
Copyright © 2011-2022 走看看