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".

如果仅仅从不断replace输入路径的角度出发,会非常复杂。

如果用一个栈来一次存储各级路径的directory名字,然后重组,会简便一些,这也是文件路径简化类题目的常用思路。

为了末尾可以顺序遍历栈重组path,我们不用传统的stack lib,而用vector来实现栈,这样可以方便顺序遍历。

代码:

class Solution {
public:
string simplifyPath(string path) {
if(path.length() == ) return "";
vector<string> v;
int p = , start = ;
while(p < path.length()){
if(path[p] == '/') ++p;
else{
start = p;
while(p < path.length() && path[p] != '/') ++p;
string temp = path.substr(start, p-start);
if(temp == ".."){
if(!v.empty()) v.pop_back(); //遇到".."就出栈
else{
if(path[] != '/') v.push_back(temp); //如果没东西可以出,就要看path是否以根目录开头了,不是以根目录开头的话,".."要进栈的
}
}else if(temp == "."){} //遇到"."就跳过
else if(temp.length() > ){
v.push_back(temp);
}
}
}
string res = (path[] == '/' ? "/" : ""); //重组path
for(vector<string>::iterator i = v.begin(); i < v.end(); ++i){
res.append(*i);
if(i < v.end()-) res.append("/");
}
return res;
}
};

[LeetCode] Simplify Path,文件路径简化,用栈来做的更多相关文章

  1. Simplify Path(路径简化)

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

  2. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  3. [LeetCode] Simplify Path 简化路径

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

  4. [leetcode]Simplify Path @ Python

    原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...

  5. [LeetCode] Coin Path 硬币路径

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

  6. System.IO.Path文件路径类

    Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...

  7. Leetcode Simplify Path

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

  8. [LeetCode] Simplify Path(可以不用看)

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

  9. [leetcode]112. Path Sum路径和(是否有路径)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. Python3实现机器学习经典算法(四)C4.5决策树

    一.C4.5决策树概述 C4.5决策树是ID3决策树的改进算法,它解决了ID3决策树无法处理连续型数据的问题以及ID3决策树在使用信息增益划分数据集的时候倾向于选择属性分支更多的属性的问题.它的大部分 ...

  2. leetcode个人题解——#11 Container with most water

    class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...

  3. 《javascript模式--by Stoyan Stefanov》书摘--字面量和构造函数

    二.字面量和构造函数 1,能够使用对象字面量时,就没理由使用new Object构造函数 // 一个空对象var 0 = new Object();console.log( o.constructor ...

  4. c# CLR无法从 COM 上下文 0x51cd20 转换为 COM 上下文 0x51ce90

    调试菜单--->异常---->managed debugging assistants栏下ContextSwitchDeadlock 前面的√去掉

  5. 【数位DP】题集

    1.[HDOJ2089] 题意:求区间内不出现4和62的数的个数 解法:模板题 2.[HDOJ3555] 题意:求区间内不出现49的数的个数 解法:模板题 3.[HDOJ5179] 题意:对于一个十进 ...

  6. 在cmd里面使用mysql命令

    1.先找出mysqld文件所在的位置,我的是在C:\Program Files\MySQL\MySQL Server 5.1\bin. 2.cd C:\Program Files\MySQL\MySQ ...

  7. 百度安卓sdk开发

    一 key问题 1 在百度地图api控制台申请key的流程主要用到了app包,开发工具的开发sha1和发布sha1值,这2个值的获取就非常关键了. 一般来说我们都是在windows上开发安卓,使用an ...

  8. 软工网络15团队作业——Alpha阶段敏捷冲刺 DAY1

    Alpha阶段敏捷冲刺 DAY1 1.各个成员在 Alpha 阶段认领的任务 姓名 在Alpha阶段所认领的任务 陈龙 题目生成类的编写,随机生成合理题目的算法编写 郑佳明 答案计算类的编写,对随机生 ...

  9. Jenkins系列-Jenkins升级、迁移和备份

    升级Jenkins Jenkins的开发迭代非常快,每周发布一个开发版本,长期支持版每半年更新一次(ps:大版本更新).如此频繁的更新,怎么升级呢? war:下载新版的war文件,替换旧版本war文件 ...

  10. G# GUID

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...