【LeetCode每天一题】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" 思路
对于python 的解法而言,我们可以先使用"/"将路径进行分割得到一个列表,设置一个辅助空间栈,然后遍历次列表,如果当前结果为'..',栈不为空弹出栈顶元素,为空则不执行操作。如果为'.',则不进行操作,否则将其添加进栈中。时间复杂度为O(n), 空间复杂度为O(n)。
解决代码
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
places = [p for p in path.split("/") if p!="." and p!=""] # 先将字符串以'/'进行分割,然后进行条件筛选。
stack = []
for p in places:
if p == "..": # 判断栈是否为空,不为空则弹出栈顶的元素
if len(stack) > 0:
stack.pop()
else:
stack.append(p)
return "/" + "/".join(stack) # 重新进行组合
【LeetCode每天一题】Simplify Path(简化路径)的更多相关文章
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- [LeetCode] Simplify Path 简化路径
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/", ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- Eclipse IDE 添加jar包到Java工程中
操作系统:Windows 10 x64 工具1:Eclipse Java EE IDE for Web Developers. Version: Photon Release (4.8.0) 在Pac ...
- gdb常用命令及gdb调试多进程/线程程序&coredump
一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...
- django第一天
今天是双十一跑了个1000很累. django 终于学到了,学了这么多天,乱七八糟的东西. 今天只是学了初级的配置文件和响应请求. 配置CSS和JS 文件目录 配置html文件目录 响应请求 配置路径 ...
- linux基础命令学习笔记(一)
2019年4月1日: “目录” = “文件夹” 常用命令(一): 1.ls: list 列表,默认当前文件夹的文件和目录 linux:命令+选项+参数 ls -l:长输出,列出文件的详细信息 - rw ...
- Spring AOP概念理解
1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后 ...
- 杭电1532----Drainage Ditches『最大流』
/* 网络流的最大流问题 刚学习Dinic算法.模版题 */ #include <cstring> #include <cstdio> #include <queue&g ...
- Python中的作用域及global用法
Python 中,一个变量的作用域总是由在代码中被赋值的地方所决定的. 函数定义了本地作用域,而模块定义的是全局作用域. 如果想要在函数内定义全局作用域,需要加上global修饰符. 变量名解析:LE ...
- Android SQL数据库应用实践 “问题点”“疑难点”“解析”
应用 Android SQL 数据库时,遇到的问题: 场景1:Android SQL查询后,获取到Cursor并查询数据:遇到以下问题:"android.database.CursorInd ...
- __x__(40)0909第五天__表格 table 的 css 样式 美化
如果就向下面的代码那样,不写 tbody , 则浏览器自添加 tbody , 并将所有的 tr 移入 tbody 意味着 tr 并非 table 的子元素,而是 tbody 的子元素. 所以 以后编写 ...
- transient关键字的使用
实例说明 在保存对象时,会将对象的状态也一并保存,然而有些状态是不应该被保存的,如表示密码的属性.此时可以使用transient关键字来修饰不想保存的属性. 关键技术 transient关键字用来防止 ...