[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
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- 100道Java面试题整理(助力2020面试!)
1.您对微服务有何了解? 微服务,又称微服务 架 构,是一种架构风格,它将应用程序构建为以业务领域为模型的小型自治服务集合 . 通俗地说,你必须看到蜜蜂如何通过对齐六角形蜡细胞来构建它们的蜂窝状物.他 ...
- MBProgressHUD覆盖键盘
发送消息的时候,需要用MBProgressHUD来覆盖窗口等待发送成功,但是无论如何键盘都覆盖不上. 于是各种研究,添加到view,添加到window,都无果..想破了脑子,总觉得加到window都覆 ...
- flask汇总
flask框架 蓝图 随着flask程序越来越复杂,我们需要对程序进行模块化的处理,之前学习过python的模块化管理,于是针对一个简单的flask程序进行模块化处理 Blueprint概念 简单来说 ...
- python使用进程池多进程时,如何打印错误信息
一.说明 1.python进程池进行多进程运行时,如果有错误,该进程会直接跳过,并且不会打印错误信息. 2.如果需要了解到进程内的错误信息,此时就需要通过捕获异常来输出错误信息了. 二.具体方法如下: ...
- 微信小程序使用第三方FontIcon库的部分字体图标
一.提取部分图标重新制作TTF字库 我没有使用网上大多数文章写的淘宝提供的fonticon,而只使用了Ionicons的几个图标,所以打开Ionicons的官网点击右上角的Designer pack下 ...
- centos快速安装mysql
1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...
- zxing生成二维码转base64 img直接显示 Image对象转Base64码(java)
public static String encodeToBase64(String content){ MultiFormatWriter multiFormatWriter = new Multi ...
- Python——课程数据统计分析
介绍 在该章节中我们将利用提供的课程数据来进行一次实战性质的时间序列和聚类分析. 知识点 数据处理 数据可视化 中文分词 文本聚类 数据概览 本次课程的数据来源于运行过程中产生的真实数据,我们对部分数 ...
- Eclipse Jee 2019-12 使用的一些小窍门(积累性更新)
在安装使用 Eclipse IDE for Enterprise Java Developers Version: 2019-12 M1 (4.14.0) 之前请确保已在系统环境变量配置好JDK1. ...
- String--课后作业1
1.程序设计思想 输入一个字符串,利用charAt()提取指定位置的字符,判断该字符是否是x,y,z(大小写都一样),若是,则将该字符减23后强制转换为char型,若不是则将其加3后强制转换为char ...