[LC] 71. Simplify Path
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的更多相关文章
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- 71. Simplify Path压缩文件的绝对路径
[抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 71. Simplify Path (Stack)
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- Linux系统相关命令
时间和日期 date cal 磁盘和目录空间 df du 进程信息 ps top kill 01. 时间和日期 序号 命令 作用 01 date 查看系统时间 02 cal calendar 查看日历 ...
- CI项目设计Redis队列
项目开发过程中需要设计提供可平衡的处理多个用户请求的队列. 需求: 当用户登录后,查看系统中已经登录的管理员队列,然后查看后台管理员的处理能力,如果已经不能处理新的请求,则把该管理员从处理队列中删除, ...
- 乳草的侵占(BFS)
armer John一直努力让他的草地充满鲜美多汁的而又健康的牧草.可惜天不从人愿,他在植物大战人类中败下阵来.邪恶的乳草已经在他的农场的西北部份占领了一片立足之地. 草地像往常一样,被分割成一个高度 ...
- 实现Action
实现Action 对于开发者来说,Action才是应用的核心,开发者需要提供大量的Action类,并在Struts.xml文件中配置Action.Action类中包含了用户请求的处理逻辑,Action ...
- 吴裕雄--天生自然ShellX学习笔记:Shell echo命令
Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo ...
- JavaEE--分布式对象
参考:http://blog.csdn.net/smcwwh/article/details/7080997 1.客户与服务器的角色 所有分布式编程技术的基本思想都很简单:客户计算机产生一个请求,然后 ...
- SQL查询出一个表数据插入到另一个表里
下面两中方式都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的: 方式一 (select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建. selec ...
- Python—守护进程管理工具(Supervisor)
一.前言简介 1.Supervisor 是一个 Python 开发的 client/server 系统,可以管理和监控类 UNIX 操作系统上面的进程.可以很方便的用来启动.重启.关闭进程(不仅仅是 ...
- Vue环境搭建-项目的创建-启动生命周期-组件的封装及应用
vue项目环境的搭建 """ node >>> python:node是用c++编写用来运行js代码的 npm(cnpm) >>> p ...
- 用PrintStream向文件输入内容
import java.io.*; public class Main { public static void main(String[] args) throws FileNotFoundExce ...