题目:

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. (KMP 根据循环节来计算)Period -- hdu -- 1358

    http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  2. 关于BufferefReader.readLine()方法的理解

    有以下代码:    BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in)); String ...

  3. CentOS7 Docker 安装

    CentOS7 已经内置了docker ,可以直接安装 安装Docker 命令: sudo yum install -y docker  启动docker  命令: service docker st ...

  4. Spark Structured Stream 2

    ❤Limitations of DStream API Batch Time Constraint application级别的设置. 不支持EventTime event time 比process ...

  5. JS——图片预览功能

    <script type="text/javascript">    function DisplayImage(fileTag) {        document. ...

  6. 自己从0开始学习Unity的笔记 I (C#字符串转换为数字)

    我基本上从0开始学习编程,运算符基本上跳过,因为知道了 “=”这个符号相当于赋值,然后“==”才是等于,其他和普通运算符号差不都,也就跳过了. 最基础的赋值那种,我看了下代码,似乎没什么难度,估计新手 ...

  7. 创建自己的Code Snippet(代码模板)

    一.名词解释 Code Snippet,代码模板,是一种快速生成代码的快捷方式,使用它可以有效地提高编程效率. 编程中可以使用Visual Studio提供的预先设置好的Code Snippet,也可 ...

  8. linux常用命令大全(linux基础命令入门到精通+命令备忘录+面试复习+实例)

    作者:蓝藻(罗蓝国度) 创建时间:2018.7.3 编辑时间:2019.4.29 前言 本文特点 授之以渔:了解命令学习方法.用途:不再死记硬背,拒绝漫无目的: 准确无误:所有命令执行通过(环境为ce ...

  9. 服务器 apache配置https,http强制跳转https(搭建http与https共存)

    公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...

  10. java实现简单扫雷游戏

    /** * 一个简单的扫雷游戏 MainFram.java */ package www.waston; import java.awt.BorderLayout; import java.awt.C ...