[leetcode]Simplify Path @ Python
原题地址: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"
- 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的更多相关文章
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] Simplify Path(可以不用看)
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- [LeetCode] Simplify Path,文件路径简化,用栈来做
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
随机推荐
- jQuery漏掉的东西
prop和attr的区别 attr一般都用来设置和操作元素的自定义属性的,而prop一般都是操作元素的内置属性的(尤其是表单元素的操作我们大部分都在使用prop) each 可以遍历jQuery集合中 ...
- HDU.5985.Lucky Coins(概率DP)
题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...
- hdu 5194 组合数学or暴力
直接凑了个公式带入,没想到直接ac了,至于题解中的期望可加性可以参考概率论相关知识 #include<cstdio> #include<iostream> #include&l ...
- 阿里云VPC默认网关问题
在使用VPC专用网络时,阿里云上面不用设置网关都可以工作,其实不是的,狗日的阿里云居然把默认网关设置成了253,比如设置了172.18.0.0/24时,那么它的默认网关就是172.18.0.253.
- STM32的PWM输入模式设置并用DMA接收数据
参考 :STM32输入捕获模式设置并用DMA接收数据 PWM input mode This mode is a particular case of input capture mode. The ...
- Golang 版本发布 与 TIOBE 排名
2016年国庆节(10月1日)开始接触 Go 语言,记录一下它的 版本发布 与 TIOBE 排名: Golang 排行榜 月份 版本 排名 备注 2012.03 1.0 201 ...
- man命令使用
如:man 2 read, 就可以查看read函数的文档
- 少女花海自拍撞亡:自拍PK火车速度,没有赢家
心理学研究,自拍是一种自我强化的过程.人们都或多或少有着自我关注的倾向,即“自恋”.而人作为有思想的群体性社会动物,有着分享和交流的欲望.尤其是现代快节奏的生活常使人感觉“亚历山大”,自拍恰恰就成为释 ...
- JAVA 对象序列化(二)——Externalizable
Java默认的序列化机制非常简单,而且序列化后的对象不需要再次调用构造器重新生成,但是在实际中,我们可以会希望对象的某一部分不需要被序列化,或者说一个对象被还原之后,其内部的某些子对象需要重新创建,从 ...
- 创建 HelloWorld 项目
在 Eclipse 的导航栏中点击 File →New →Android Application Project ,此时会弹出创建 Android 项目的对话框.其中 Application Name ...