Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
使用一个栈来解决问题。遇到'..'弹栈,遇到'.'不操作,其他情况下压栈。
代码一:
class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack = []
i =
res = ''
while i< len(path):
end = i+
while end<len(path) and path[end] !="/":
end +=
sub = path[i+:end]
if len(sub)>:
if sub == "..":
if stack !=[]:
stack.pop()
elif sub != ".":
stack.append(sub)
i = end if stack == []:
return "/"
for i in stack:
res += "/"+i
return res
code 2:
class Solution:
def simplifyPath(self,path):
path = path.split('/')
res = '/'
for i in path:
if i == '..':
if res != '/':
res = '/'.join(res.split('/')[:-1])
if res =='': res = '/'
elif i != '.' and i != '':
res += '/' +i if res != '/' else i
return res
转自(参考):
1. http://www.cnblogs.com/zuoyuan/p/3777289.html
2. http://blog.csdn.net/linhuanmars/article/details/23972563
@ JAVA 版本
public String simplifyPath(String path) {
if(path == null || path.length()==0)
{
return "";
}
LinkedList<String> stack = new LinkedList<String>();
StringBuilder res = new StringBuilder();
int i=0;
while(i<path.length())
{
int index = i;
StringBuilder temp = new StringBuilder();
while(i<path.length() && path.charAt(i)!='/')
{
temp.append(path.charAt(i));
i++;
}
if(index!=i)
{
String str = temp.toString();
if(str.equals(".."))
{
if(!stack.isEmpty())
stack.pop();
}
else if(!str.equals("."))
{
stack.push(str);
}
}
i++;
}
if(!stack.isEmpty())
{
String[] strs = stack.toArray(new String[stack.size()]);
for(int j=strs.length-1;j>=0;j--)
{
res.append("/"+strs[j]);
}
}
if(res.length()==0)
return "/";
return res.toString();
}
Leetcode 之Simplify Path @ python的更多相关文章
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- [LeetCode] 71. 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/& ...
- Java for LeetCode 071 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- leetcode[70] Simplify Path
题目的意思是简化一个unix系统的路径.例如: path = "/home/", => "/home"path = "/a/./b/../../ ...
- Leetcode 之Simplify Path(36)
主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...
- 【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 ...
随机推荐
- DevExpress v18.1新版亮点——WinForms篇(二)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v18.1 的新功能,快来下载试用新版本! ...
- Eclipse js报错问题解决办法
最近在Eclipse中导入新项目后会发现js报错,但是不影响程序的运行,但是对于程序员的我们来说多少还是比较在意代码前面的红色的X的,有木有??? 上网也查了很多方法,对于其中一种方法表示不能完全解决 ...
- IOS常用代码整理
常用代码整理: 12.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRege ...
- FZU 1202
http://acm.fzu.edu.cn/problem.php?pid=1202 二分图最大匹配,问哪些边是必要的,O(n^3)的方法 删边的时候把连接关系也要删掉,如果在此基础上无法找到增广路, ...
- linux配置禁用启用IPv6
IPv6被认为是IPv4的替代产品,它用来解决现有IPv4地址空间即将耗尽的问题.但目前,开启IPv6可能会导致一些问题.因此有时我们需要关闭IPv6.下面是IPv6的关闭方法应该适用于所有主流的Li ...
- win10笔记本实现双屏显示的自如切换
前言 使用电脑的过程中想一边看内容,一边进行编辑,这就涉及到双屏显示并实现扩展分屏,本文就介绍一下这些操作. 工具 win10-thinkpad-E470:另一块显示屏(博主的是戴尔的显示器):一条外 ...
- rabbitmq学习(五):springboot整合rabbitmq
一.Springboot对rabbitmq的支持 springboot提供了对rabbitmq的支持,并且大大简化了rabbitmq的相关配置.在springboot中,框架帮我们将不同的交换机划分出 ...
- HDU 4632 区间DP 取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...
- BZOJ1835,LG2605 [ZJOI2010]基站选址
题意 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为\(D_i\) 需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为\(C_i\) 如果在距离第i个村 ...
- hasura graphql-engine v1.0.0-alpha26 版本新功能
hasura 发布了graphql-engine v1.0.0-alpha26 版本,有一些破坏的变动,以及方便的新特性 破坏性变动 order_by 从 order_by: id_asc 为 ord ...