【字符串】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 = & ...
随机推荐
- MATLAB中的快捷键
Ctrl + c 中止程序的运行,鼠标要点到命令窗内.
- 20155218 2016-2017-2 《Java程序设计》第8周学习总结
20155218 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可以 ...
- (线段树 点更新 区间求和)lightoj1112
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/D (密码0817) Description Robin ...
- java 泛型的类型擦除和桥方法
oracle原文地址:https://docs.oracle.com/javase/tutorial/java/generics/erasure.html 在Java中,泛型的引入是为了在编译时提供强 ...
- hdu 2149
题目 巴什博奕(Bash Game) 巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一 ...
- hdu 5019
http://acm.hdu.edu.cn/showproblem.php?pid=5019 给出X 和Y,求出第K 大的 X 和Y 的公约数. 暴力求出所有公约数 #include <cstd ...
- 摘抄-----java codeReview要做的事
整洁的代码 清单项目 分类 使用可以表达实际意图(Intention-Revealing)的名称 有意义的名称 每一个概念只用一个词 有意义的名称 使用方案/问题领域名称 有意义的名称 类应该是比较小 ...
- .NET Core 运行时标识符 (RID) 目录
RID 是什么? RID 是运行时标识符的缩写. RID 用于标识其中将运行应用程序或资产(即程序集)的目标操作系统. 其外观类似如下:“ubuntu.14.04-x64”.“win7-x64”.“o ...
- .net core An assembly specified in the application dependencied mainfest<****.json>was not found解决办法
最近在开发项目中,遇到了一个问题.在本机开发中部署到本机iis上或者本机控制台都没有问题,运行正常.当发布部署到服务器(windowsServer)中的时候一直运行不起来,用控制台也运行不起来,直接报 ...
- EF 多对多循环引用序列化失败 解决办法
错误:Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.tbldph_901D48A194FB31 ...