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(简化路径)的更多相关文章

  1. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  2. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  3. [LeetCode] Simplify Path 简化路径

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

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

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

  5. 071 Simplify Path 简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...

  6. Leetcode71. Simplify Path简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...

  7. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...

  8. LeetCode(71) Simplify Path

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

  9. LeetCode OJ:Simplify Path(简化路径)

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

随机推荐

  1. C语言对字符串去重

    # include <stdio.h> # include <string.h> char * getNewChar(char * str,char * newStr); in ...

  2. python中关于变量名失效的案例

    案例一:传参动态导入模块. selectModule = input("please input your module name") app_name = input(" ...

  3. git官网和安装使用教程链接

    git官网 https://git-scm.com/download/win git安装教程 https://www.cnblogs.com/wj-1314/p/7993819.html

  4. 项目启动,main函数之前的代码执行两次 restartedMain

    https://blog.csdn.net/qq_35981283/article/details/78925146

  5. git本地项目上传至码云gitee

    如果你的本机是安装成功第一次使用,先配置一下一些基本的信息 $ git config--global user.name "Your Name" $ git config --gl ...

  6. Redis数据结构之robj

    本文及后续文章,Redis版本均是v3.2.8 我们知道一个database内的这个映射关系是用一个dict来维护的.dict的key固定用一种数据结构来表达,这这数据结构就是动态字符串sds.而va ...

  7. CodeForces 286E Ladies' Shop 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...

  8. iOS12系统应用发送邮件中的附件

    iOS12系统应用发送邮件中的附件 iOS12系统应用发送邮件中的附件,如果发送邮件的内容很多,或者包含文档.图片等,可以以附件的形式进行发送.此时需要使用addAttachmentData(_:mi ...

  9. lArea.js 城市选择

    http://blog.csdn.net/libin_1/article/details/50689075 lArea.js

  10. ElasticSearch 6.4.3 启动报错: [Cannot assign requested address: bind]

    今天在本地搭建一个测试用的最新版ElasticSearch6.4.3 的环境时,遇到一个报: [Cannot assign requested address: bind]的错误. 错误日志内容如下: ...