[LeetCode] 71. Simplify Path

Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

The canonical path should have the following format:

  • The path starts with a single slash '/'.
  • Any two directories are separated by a single slash '/'.
  • The path does not end with a trailing '/'.
  • The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')

Return the simplified canonical path. 

Example 1:

Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

Example 2:

Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.

Example 3:

Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

Example 4:

Input: path = "/a/./b/../../c/"
Output: "/c" 

Constraints:

  • 1 <= path.length <= 3000
  • path consists of English letters, digits, period '.', slash '/' or '_'.
  • path is a valid absolute Unix path.

简化路径。

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/simplify-path
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个Unix环境下的绝对路径,请简化之。

思路是 stack 栈。将 input 按 '/' 分成一个个小的部分,然后判断每个部分是否需要放进栈。遇到点(.)和空的字符串(' ')就跳过不放,遇到两个点(..)不仅不放反而要弹出之前放的部分;其他情况就正常放进栈。最后再将栈内的元素拼接成字符串输出。

时间O(n)

空间O(n)

JavaScript实现

 1 /**
 2  * @param {string} path
 3  * @return {string}
 4  */
 5 var simplifyPath = function (path) {
 6     let stack = [];
 7     path = path.split('/');
 8     for (let i = 0; i < path.length; i++) {
 9         if (path[i] == '.' || path[i] == '') continue;
10         if (path[i] == '..') {
11             stack.pop();
12         } else {
13             stack.push(path[i]);
14         }
15     }
16     return '/' + stack.join('/');
17 };

Java实现

 1 class Solution {
 2     public String simplifyPath(String path) {
 3         Stack<String> stack = new Stack<>();
 4         String[] paths = path.split("/+");
 5         for (String s : paths) {
 6             if (s.equals("..")) {
 7                 if (!stack.isEmpty()) {
 8                     stack.pop();
 9                 }
10             } else if (!s.equals(".") && !s.equals("")) {
11                 stack.push(s);
12             }
13         }
14         String res = "";
15         while (!stack.isEmpty()) {
16             res = "/" + stack.pop() + res;
17         }
18         if (res.length() == 0) {
19             return "/";
20         }
21         return res;
22     }
23 }

 注意16行Java最后整理res的过程,其实是类似这样的。最外层的路径先进stack,最内层的路径先从stack弹出。而JS则不需要这样做,JS可以直接把array里面的元素concat成string输出。

example, "/a/b/c"

stack: [a, b, c]

/c
/b/c
/a/b/c

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12302063.html