71-简化路径(Simplify Path)

痛定思痛。 2022-04-25 06:40 317阅读 0赞

题目描述
中文

  1. Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。
  2. Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者
  3. 都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径
  4. 请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能以 /
  5. 尾。此外,规范路径必须是表示绝对路径的最短字符串。

英文

  1. Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the
  2. canonical path.
  3. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double
  4. period .. moves the directory up a level. For more information, see: Absolute path vs relative path
  5. in Linux/Unix
  6. Note that the returned canonical path must always begin with a slash /, and there must be only a
  7. single slash / between two directory names. The last directory name (if it exists) must not end with
  8. a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

示例1

  1. 输入:"/home/"
  2. 输出:"/home"
  3. 解释:注意,最后一个目录名后面没有斜杠。

示例2

  1. 输入:"/../"
  2. 输出:"/"
  3. 解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。

示例3

  1. 输入:"/home//foo/"
  2. 输出:"/home/foo"
  3. 解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。

示例4

  1. 输入:"/a/./b/../../c/"
  2. 输出:"/c"

示例5

  1. 输入:"/a/../../b/../c//.//"
  2. 输出:"/c"

示例6

  1. 输入:"/a//bc/d//././/.."
  2. 输出:"/a/b/c"

思路分析

  1. 1.好吧,我现在也没什么思路...可不可以尝试用暴力的方法做呢?暴力+if判断是否可行呢?我们来试一下。
  2. class Solution {
  3. public String simplifyPath(String path) {
  4. String[] temp = path.split("\\/"); //用将字符串切割
  5. List<String> list = new ArrayList<String>(); //存放绝对路径目录
  6. for(String s:temp){
  7. if(s.length() != 0){
  8. if(s.equals("..")){ //如果是返回上层 且可以返回
  9. if(list.size() > 0){
  10. list.remove(list.size()-1);
  11. }
  12. else{
  13. continue;
  14. }
  15. }
  16. else if(s.equals(".")){
  17. continue;
  18. }
  19. else{
  20. list.add(s);
  21. }
  22. }
  23. }
  24. String res = new String();
  25. for(String s:list){
  26. res = res + "/" + s;
  27. }
  28. if(res.length() == 0) //如果最后路径为空,返回根目录
  29. res = "/";
  30. return res;
  31. }
  32. }
  • 很明显,是可以的,解决过程就是先将字符串以”/“切割开,然后进行逻辑判断,最后拼接路径就可以解决了。现在让我们用python3再写一次。

    class Solution:

    1. def simplifyPath(self, path: str) -> str:
    2. strings = path.split("/")
    3. paths = []
    4. for string in strings:
    5. if len(string) != 0:
    6. if string == "..":
    7. if len(paths) > 0:
    8. del paths[len(paths) - 1]
    9. else:
    10. continue
    11. elif string == '.':
    12. continue
    13. else:
    14. paths.append(string)
    15. res = ""
    16. for path in paths:
    17. res = res + "/" + path
    18. if len(res) == 0:
    19. res = "/"
    20. return res
  • 但是有没有更为聪明的方法呢?emmm,我暂时想不到,咱们来看下别人的代码

  • 额。好吧,好像大家思路都一样,只不过是用栈来做的,打扰了,今天就到这里。

发表评论

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

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

相关阅读

    相关 71. 简化路径

      > 给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。 > > 在 Unix 风格

    相关 LeetCode71简化路径

    71. 简化路径 1. 问题重述 将Unix风格的文件路径简化及合法化 , 防止用户恶意输入路径 > 请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录