【字符串】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"
.
思路:
字符串处理,由于".."是返回上级目录(如果是根目录则不处理),因此可以考虑用栈记录路径名,以便于处理。需要注意几个细节:
- 重复连续出现的'/',只按1个处理,即跳过重复连续出现的'/';
- 如果路径名是".",则不处理;
- 如果路径名是"..",则需要弹栈,如果栈为空,则不做处理;
- 如果路径名为其他字符串,入栈。
最后,再逐个取出栈中元素(即已保存的路径名),用'/'分隔并连接起来,不过要注意顺序呦。
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function(path) {
var stack=[],len=path.length,i=0;
while(i<len){
//跳过开头的'/''
while(path[i]=='/'&&i<len){
i++;
} var s='';
while(i<len&&path[i]!='/'){
s+=path[i++];
} //如果是".."则需要弹栈,否则入栈
if(".." == s && stack.length!=0){
stack.pop();
}else if(s != "" && s != "." && s != ".."){
stack.push(s);
} } //如果栈为空,说明为根目录,只有斜线'/'
if(stack.length==0){
return '/'
}
var res='';
while(stack.length!=0){
res = "/" + stack.pop() + res;
}
return res;
};
【字符串】Simplify Path(栈)的更多相关文章
- Simplify Path——简单经典的预处理
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- 【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 ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
随机推荐
- (最小生成树) Networking -- POJ -- 1287
链接: http://poj.org/problem?id=1287 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7494 ...
- java web 通过前台输入的数据(name-value)保存到后台 xml文件中
一:项目需求,前端有一个页面,页面中可以手动输入一些参数数据,通过点击前端的按钮,使输入的数据保存到后台生成的.xml文件中 二:我在前端使用的是easyui的propertygrid,这个能通过da ...
- UniGui之锱铢积累(仔细看这个文件)
http://www.doc88.com/p-4022977294324.html 这个是Word文档
- 【转】Swig使用指南
如何使用 API swig.init({ allowErrors: false, autoescape: true, cache: true, encoding: 'utf8', filters: { ...
- TempDB--临时表的缓存
--========================================================================== 在博客园看到一篇文章<SQLServer ...
- C#实现在图片上动态写内容
之前在项目上遇到这么一个需求,就是要在图片上写内容,而且要求是动态,我所谓的动态就是在图片上写的内容是动态的.网上找了找,很多人实现了网图片上写内容的功能,但是,并没有实现动态.所以在这里把我的解决办 ...
- (zxing.net)一维码MSI的简介、实现与解码
一.简介 MSI/Plessey 条码(也被称为 MSI 或 Modified Plessey)是一款数字条码,多用于超市.存储用的仓库和其他贮藏室的货架.货架上的条码可以告知货架上的产品.应放数量和 ...
- 2D Polygons( Poygon) CGAL 4.13 -User Manual
1 Introduction A polygon is a closed chain of edges. Several algorithms are available for polygons. ...
- eclipse创建Maven工程没有Maven Dependencies
记一次eclipse创建Maven工程没有Maven Dependencies的坑 Eclipse版本:luna版 maven用的3.5.0 配置本文不提. 这辆天用Eclipse创建maven工程 ...
- iOS App的加固保护原理
本文由 网易云发布. 本文从攻防原理层面解析了iOS APP的安全策略.iOS以高安全性著称,但它并非金刚不坏之身.对于信息安全而言,止大风于青萍之末是上上策,杭研深入各个细节的研发工作,正是网易产 ...