题目:

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:

  • 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. 重复连续出现的'/',只按1个处理,即跳过重复连续出现的'/';
  2. 如果路径名是".",则不处理;
  3. 如果路径名是"..",则需要弹栈,如果栈为空,则不做处理;
  4. 如果路径名为其他字符串,入栈。

最后,再逐个取出栈中元素(即已保存的路径名),用'/'分隔并连接起来,不过要注意顺序呦。

/**
* @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(栈)的更多相关文章

  1. Simplify Path——简单经典的预处理

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  2. LeetCode(71) Simplify Path

    题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...

  3. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...

  4. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

  6. 【LeetCode】71. Simplify Path

    Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...

  7. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  8. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

  9. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

随机推荐

  1. golang闭包里的坑

    介绍 go的闭包是一个很有用的东西.但是如果你不了解闭包是如何工作的,那么他也会给你带来一堆的bug.这里我会拿出Go In Action这本书的一部分代码,来说一说在使用闭包的时候可能遇到的坑.全部 ...

  2. SQLite数据库下载、安装和学习

    SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠.与其他数据库管理系统不同,SQLite 的安装和运行非常 ...

  3. spring读取数据库的配置信息(url、username、password)时的<bean>PropertyPlaceholderConfigurer的用法

    用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  4. Alwayson--问题总结一

    1. Alwayson 是否依赖于域环境? 答: 是, alwayson依赖于故障转移群集(只有在故障转移群集中的SQL Server 才能启动高可行性组功能),而故障转移群集愈依赖于域环境. 2. ...

  5. rtmp官方标准规范详细解析

    标准规范学习: rtmp消息结构,包括几个部分: 时戳:4  byte,单位毫秒.超过最大值后会翻转. 长度:消息负载的长度. 类型ID:Type Id 一部分ID范围用于rtmp的控制信令.还有一部 ...

  6. dropdownlist的OnSelectedIndexChanged方法不触发

    解决方法: AutoPostBack="true" if (!IsPostBack){ 数据绑定 } 如果数据绑定后不触发,但是手动添加数据的情况下触发<%@ Page En ...

  7. SSM文件上传

    **自己对于SSM文件上传的一些心得** 刚开始的时候也是在网上寻找一些简单的案例,可能我的这篇文章不是最好的,但是这些都是我自己慢慢的摸索以及自己的尝试的一些心得,希望对各位有所帮助. 其实文件的上 ...

  8. Spring 开发第一步(四)Spring与JDBC事务

    Spring使用各种不同的TransactionManager来管理各种不同数据源事务底层(比如jdbc数据源.hibernate数据源.JPA数据源等等).在此基础上使用各种对应的Template来 ...

  9. win10 下Oracle安装

    在win10 64bit下安装oracle 11的时候,有些问题不注意,安装的时候才遇到就gg了.下面将介绍win10下安装oracle 11的步骤以及安装过程中遇到的一些问题及解决方案. 安装过程中 ...

  10. jzoj5806

    我們發現,如果有奇環顯然無解,因為我們每一次合併一個奇環,一定會產生一個偶環和一個更小的奇環,最終會形成一個三元環而無法合併 所以,這個圖需要是一個二分圖,需要進行二染色 然後把這個圖所有的偶環找出來 ...