LeetCode - Medium - 71. Simplify Path

ゞ 浴缸里的玫瑰 2022-11-17 05:27 284阅读 0赞

Topic

  • String
  • Stack

Description

https://leetcode.com/problems/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:

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

Example 2:

  1. Input: path = "/../"
  2. Output: "/"
  3. 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:

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

Example 4:

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

Constraints:

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

Analysis

Submission

  1. import java.util.LinkedList;
  2. public class SimplifyPath {
  3. // 方法一:我写的
  4. public String simplifyPath(String path) {
  5. LinkedList<String> dirs = new LinkedList<>();
  6. for (int p1 = 0, p2 = 1; p2 <= path.length(); p2++) {
  7. if (p2 == path.length() || path.charAt(p2) == '/') {
  8. String dir = path.substring(p1 + 1, p2);
  9. p1 = p2;
  10. if (dir.equals("") || dir.equals("."))
  11. continue;
  12. if (dir.equals("..")) {
  13. if (!dirs.isEmpty())
  14. dirs.removeLast();
  15. } else {
  16. dirs.add(dir);
  17. }
  18. }
  19. }
  20. StringBuilder sb = new StringBuilder();
  21. dirs.stream().forEach(dir -> sb.append("/").append(dir));
  22. return sb.length() == 0 ? "/" : sb.toString();
  23. }
  24. // 方法二:别人写的,写得更简洁
  25. public String simplifyPath2(String path) {
  26. LinkedList<String> list = new LinkedList<>();
  27. for (String cur : path.split("/")) {
  28. if (cur.equals("..")) {
  29. if (!list.isEmpty())
  30. list.removeLast();
  31. } else if (cur.length() > 0 && !cur.equals("."))
  32. list.add(cur);
  33. }
  34. return "/" + String.join("/", list);
  35. }
  36. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. public class SimplifyPathTest {
  4. @Test
  5. public void test() {
  6. SimplifyPath obj = new SimplifyPath();
  7. assertEquals("/home", obj.simplifyPath("/home/"));
  8. assertEquals("/", obj.simplifyPath("/../"));
  9. assertEquals("/home/foo", obj.simplifyPath("/home//foo/"));
  10. assertEquals("/c", obj.simplifyPath("/a/./b/../../c/"));
  11. assertEquals("/home", obj.simplifyPath2("/home/"));
  12. assertEquals("/", obj.simplifyPath2("/../"));
  13. assertEquals("/home/foo", obj.simplifyPath2("/home//foo/"));
  14. assertEquals("/c", obj.simplifyPath2("/a/./b/../../c/"));
  15. }
  16. }

发表评论

表情:
评论列表 (有 0 条评论,284人围观)

还没有评论,来说两句吧...

相关阅读