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. Go mod graphql-go 的 Replace

    现在在项目中大量的使用 graphql,但用的版本是3年前的版本. 3年前包的url:github.com/neelance/graphql-go 现在的url:github.com/graph-go ...

  2. FileReader笔记

    FileReader API链接地址:https://developer.mozilla.org/en-US/docs/Web/API/FileReader 实例代码: <!DOCTYPE ht ...

  3. thinkcmf面包屑制作

    网站常有的功能面包屑,如图所示: 支持1级分类 list.html 首页 / {$name} article.html 首页 / {$term['name']} / {$post_title} 链接: ...

  4. ubuntu14.10安装gitlab

    1 换源: # curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/ ...

  5. 干货 | 调用AI api 实现网页文字朗读

    京东云上提供了足够多的人工智能api,并且都使用了http的方式进行了封装,用户可以方便在自己的系统中接入京东云的ai能力.今天就是介绍一下如何编写很少的代码就能使用京东云的语音合成api在网页中实现 ...

  6. 微服务项目开发学成在线_day03 CMS页面管理开发

    springboot引入mangodb依赖坐标:在spring-boot集成条件下,使用mongodb的DAO层开发. swagger查看接口文档,请求地址:http://localhost:3100 ...

  7. vscode template中设置不换行

    { "workbench.colorTheme": "Dark-Dracula", "workbench.iconTheme": " ...

  8. 常见的nosql数据库有哪些?以及他们的特点与区别?

    一.常见的nosql 二.Redis,Memcache,MongoDb的特点 (1).Redis 优点: 1.支持多种数据结构,如 string(字符串). list(双向链表).dict(hash表 ...

  9. Maven--Eclipse maven相关配置

    选择自己安装的 Maven 版本: 更改配置文件路径,这里选择自己安装的 Maven 下的配置文件,方便配置及统一控制:

  10. let和var的区别

    在JavaScript中,定义变量的关键词一般用var,但还有一种定义变量的关键词叫let.两者的作用域范围不一样,我们可以将var理解为定义的是一个全局变量,而let定义的是一个局部变量.故let常 ...