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. Redis在CentOS和Windows安装过程

    redis是一种key-value高效的内存数据库. key-value是什么?json懂吧?字典懂吧?这些都是key-value结构的数据,每个key对应1个value. 那这个数据库和我们网站在使 ...

  2. Linux--奇思淫才

    根据进程号找到可执行的文件路径 [ec2-user@baolin ~]$ ll /proc/<pid>/exe lrwxrwxrwx 1 ec2-user ec2-user 0 May 3 ...

  3. jsp中静态include和动态include的区别

    jsp中静态include和动态include的区别 动态 INCLUDE 用 jsp:include 动作实现 <jsp:include page="included.jsp&quo ...

  4. yii2的csrf验证原理分析及token缓存解决方案

    本文主要分三个部分,首先简单介绍csrf,接着对照源码重点分析一下yii框架的验证原理,最后针对页面缓存导致的token被缓存提出一种可行的方案.涉及的知识点会作为附录附于文末. 1.CSRF描述 C ...

  5. Codeforces 1110D. Jongmah 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1110D.html 题意 给定 n 个数,每一个数都是在 [1,m] 里的整数. 从中取出形如 {x,x,x ...

  6. C#细说多线程

    引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发.其中委托的BeginInvoke方法以及回调函数最为常用.而 I/O线程可能 ...

  7. Java菜鸟浅谈OCR

    什么是OCR? 粗暴点说就是图片文本识别!正规点的说法就是:(Optical Character Recognition,光学电子识别) 最近公司开展新项目,考虑到实名认证这方面,然后还要上传身份证正 ...

  8. Do-Now—团队Scrum 冲刺博客五

    各个成员今日完成的任务 侯泽洋: 每日任务页面编写,任务修改功能 周亚杰:完成个人中心页面设计 王志伟:完成个人中心页面设计 唐才铭:启动动画及引导页与项目合并 项目燃尽图 站立式会议照片 各个成员遇 ...

  9. Oracle命令行中显示:ORA-04076: 无效的 NEW 或 OLD 说明

    Oracle命令行进行操作时可能出现"ORA-04076: 无效的 NEW 或 OLD 说明" 需要在条件语句中JOB前面添加“old.”即可(因为是在when条件里面,所以不用“ ...

  10. HTML5_音视频标签 <audio> 和 <video>

    HTML5_音视频标签 <audio> 和 <video> audio 和 video 都是 inline行内元素 如果浏览器支持,则不显示标签文本 IE8 不支持 audio ...