Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.

In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix

Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

Example 1:

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

Example 2:

Input: "/../"
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: "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

Example 4:

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

Example 5:

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

Example 6:

Input: "/a//b////c/d//././/.."
Output: "/a/b/c"
class Solution:
def simplifyPath(self, path: str) -> str:
import re
lst = re.split('/+', path)
stack = []
for item in lst:
if item == '..':
if stack:
stack.pop()
elif item and item != '.':
stack.append(item)
else:
continue
res = '/' + '/'.join(stack)
return res

[LC] 71. Simplify Path的更多相关文章

  1. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

  2. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】71. Simplify Path

    Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...

  4. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

  5. 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  6. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 71. Simplify Path (Stack)

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  8. [LeetCode] 71. Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  9. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

随机推荐

  1. MySQL--通过.frm和.ibd对mysql数据恢复

    转载:http://bbs.csdn.net/topics/392114182 例如说 现在要恢复user表1.先建立和之前user表一样的表结构.就是执行create table user .... ...

  2. awk中传参方式

    结合编辑数据文件的shell脚本学习awk传参方式,该脚本功能: a.取VIDEOUSR_11082017_0102_ONLINE_STASTIC.dat文件中第87个字段的低8位: b.将每行数据的 ...

  3. 关于scala工程结构(使用sbt)

    scala_project:常用目录结构: |lib:手动添加依赖包 |project | |build.properties:build的版本号,可以不写,会自动下载 | |plugins.sbt: ...

  4. 题解【[FJOI2018]所罗门王的宝藏】

    本题解同步于luogu emmm切了近年省选题来写题解啦qwq 该题较其他省选题较水吧(否则我再怎么做的出来 思路是图论做法,做法上楼上大佬已经讲的很清楚了,我来谈谈代码实现上的一些细节 \[\tex ...

  5. IDEA忽略文件,防止git提交不想提交的文件

    IDEA忽略文件,防止git提交不想提交的文件 方法一(只对没有add到仓库的文件有效): 方法二(只对没有add到仓库的文件有效): 在IDEA中安装.ignore插件.创建好了之后: 安装.git ...

  6. Django的模板变量

    变量的值来自context中的数据字典, 类似于字典对象的keys到values的映射关系. 变量是被}}和{{括起来的部分 变量看起来就像是这样: {{ variable }}. 当模版引擎遇到一个 ...

  7. POJ 1860 Currency Exchange【bellman-Ford模板题】

    传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...

  8. gcd--最大公因数

    求两个数的最大公倍数 考完五校的第一天,在家补视频ing,简单来说的话就是给了两个数A,B 假设他们两个的最大公倍数为d,那么A=X*d,B=Y*d gcd就是把一直gcd(B%A,A)不断更新,其中 ...

  9. 【MySQL参数】- query_cache_type

    MySQL为什么要关闭查询缓存 https://blog.csdn.net/liqfyiyi/article/details/50178591 Query cache的优化方法 https://blo ...

  10. 1.Redis简介/配置文件

    redis简介 redis使用入门 redis安装 http://www.runoob.com/redis/redis-install.html [root@yz---- bin]# ll total ...