原题地址:https://oj.leetcode.com/problems/simplify-path/

题意:

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".
解题思路:

题目的要求是输出Unix下的最简路径,Unix文件的根目录为"/","."表示当前目录,".."表示上级目录。

例如:

输入1:

/../a/b/c/./..

输出1:

/a/b

模拟整个过程:

1. "/" 根目录

2. ".." 跳转上级目录,上级目录为空,所以依旧处于 "/"

3. "a" 进入子目录a,目前处于 "/a"

4. "b" 进入子目录b,目前处于 "/a/b"

5. "c" 进入子目录c,目前处于 "/a/b/c"

6. "." 当前目录,不操作,仍处于 "/a/b/c"

7. ".." 返回上级目录,最终为 "/a/b"

使用一个栈来解决问题。遇到'..'弹栈,遇到'.'不操作,其他情况下压栈。

代码一:

class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack = []
i = 0
res = ''
while i < len(path):
end = i+1
while end < len(path) and path[end] != "/":
end += 1
sub=path[i+1:end]
if len(sub) > 0:
if sub == "..":
if stack != []: stack.pop()
elif sub != ".":
stack.append(sub)
i = end
if stack == []: return "/"
for i in stack:
res += "/"+i
return res

代码二:

利用python的字符串处理能力。

class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
path = path.split('/')
curr = '/'
for i in path:
if i == '..':
if curr != '/':
curr = '/'.join(curr.split('/')[:-1])
if curr == '': curr = '/'
elif i != '.' and i != '':
curr += '/' + i if curr != '/' else i
return curr

[leetcode]Simplify Path @ Python的更多相关文章

  1. Leetcode 之Simplify Path @ python

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

  2. [LeetCode] Simplify Path 简化路径

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

  3. Leetcode Simplify Path

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

  4. [LeetCode] Simplify Path(可以不用看)

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

  5. [LeetCode] Simplify Path,文件路径简化,用栈来做

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

  6. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

  8. 【LeetCode】71. Simplify Path

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

  9. [LintCode] Simplify Path 简化路径

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

随机推荐

  1. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  2. ==与equals与hashCode的区别联系。

    前言:对于引用类型的变量,它涉及到两块内存,一块是堆中的内存,用于存放new出来的对象(就是对象的具体内容):另一块是栈内存,用来存放变量在堆内存中的地址. 1,“==” 判断两个变量是否是同一个变量 ...

  3. NXP LPC-Link LPC3154

    LPC-Link: LPC-Link调试探针由恩智浦.Code Red和Embedded Artists三方共同开发, 该探针可与目标板断开,利用板载10针JTAG/SWD连接器直接用于客户自己的设计 ...

  4. android 控件: xml 设置 Button 按下背景

    本篇文章讲述了不使用java代码来改变 Button 按下和未按下时的背景. 首先准备两张图片, 分别是按钮按下和按钮未按下的. 在res/drawable 文件夹中创建一个button_select ...

  5. mysql exists 如何使用

    还没时间看,exists用的少  ==>当你只需要判断后面的查询结果是否存 在时使用exists() http://edu.codepub.com/2011/0208/29218.php 今天正 ...

  6. Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包

    Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...

  7. 基于开源SuperSocket实现客户端和服务端通信项目实战

    一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...

  8. 使用JavaScript的数组实现数据结构中的队列与堆栈

    今天在项目中要使用JavaScript实现数据结构中的队列和堆栈,这里做一下总结. 一.队列和堆栈的简单介绍 1.1.队列的基本概念 队列:是一种支持先进先出(FIFO)的集合,即先被插入的数据,先被 ...

  9. Visual Studio 2015 update 2 setup fails with "missing or damaged package kb3022398"

    Question     Hi, I wanted to install Visual Studio Professional 2015 Update 2 from my MSDN abo (web ...

  10. 在Delphi中操作快捷方式

    快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...