[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 ...
随机推荐
- collectionFramwork-1
1. Set.List和Map可以看做集合的三大类. List集合是有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问. Set集合是无序集合,集合中的元素不可以重复,访问集合中 ...
- centos7 静默安装oracle
系统centos7.4 mini 关闭selinux.firewalld 配置主机名: hostnamectl set-hostname --static oracle 之前说oracle不认cen ...
- code vs 1094 FBI树 2004年NOIP全国联赛普及组
题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...
- [BZOJ3757]苹果树(树上莫队)
树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...
- app分组
将项目中中的urls.py复制到app当中 清空项目名称文件夹下的urls.py文件中的内容,并写入一下内容 from django.conf.urls import url,include urlp ...
- tesseract-ocr识别中文扫描图片实例讲解
当我浏览http://code.google.com/p/tesseract-ocr并下载了几个文件下来之后顿时感到一头雾水,不知该如何下手.网上看到有人在linux操作系统下的实现, 如: 利用开源 ...
- WebClient 通过get和post请求api
//get 请求 string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b ...
- 【Android基础篇】TabWidget设置背景和字体
在使用TabHost实现底部导航栏时,底部导航栏的三个导航button无法在布局文件中进行定制.比方设置点击时的颜色.字体的大小及颜色等,这里提供了一个解决的方法.就是在代码里进行定制. 思路是在Ac ...
- 在Visual Studio中使用用例图描述系统与参与者间的关系
"用例图"用来描述谁用系统,用系统做什么.用例图不涉及使用细节,只用来描述使用人员和系统的关系,也不涉及行动的顺序.一起来体验. 使用Visual Studio 2012创建解决方 ...
- 项目从.NET 4.5迁移到.NET 4.0遇到的问题
当把项目从.NET 4.5迁移到.NET 4.0时,遇到的问题和解决如下: 在"属性--应用程序--目标框架"设置成.NET 4.0版本. 重新生成项目,报有关EF的错: 卸载掉项 ...