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

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

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

题目不难,主要考虑一些特殊情况。

对于path = "/a/./b/../../c/", => "/c",模拟一下

先按照'/'对字符串进行分割,得到 [a, . , b, .. , .. , c]

首先进入目录a,注意 '.' 代表当前目录 ,".."代表上一个目录

然后到达'.',还是在当前目录,/a

然后到达'b',这为/a/b

然后到达'..',这是回到父目录,则变为/a

然后到达'..',继续回到父目录,则变为/

然后到达'c',则达到子目录,变为/c

class Solution {
public:
vector<string> split(string& path, char ch){
int index = ;
vector<string> res;
while(index < path.length()){
while(index < path.length() && path[index] == '/') index++;
if(index >= path.length()) break;
int start=index, len = ;
while(index < path.length() && path[index]!='/') {index++;len++;}
res.push_back(path.substr(start,len));
}
return res;
} string simplifyPath(string path) {
vector<string> a = split(path,'/');
vector<string> file;
for(int i = ; i < a.size(); ++ i){
if(a[i] == ".." ){
if(!file.empty()) file.pop_back();
}
else if(a[i]!=".") file.push_back(a[i]);
}
string res="";
if(file.empty()) res ="/";
else{
for(int i = ; i < file.size(); ++ i) res+="/"+file[i];
}
return res;
}
};

Leetcode Simplify Path的更多相关文章

  1. [LeetCode] Simplify Path 简化路径

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

  2. [leetcode]Simplify Path @ Python

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

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

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

  4. [LeetCode] Simplify Path,文件路径简化,用栈来做

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

  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 解题报告(Python)

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

  7. 【LeetCode】71. Simplify Path

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

  8. [LintCode] Simplify Path 简化路径

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

  9. 56. Edit Distance && Simplify Path

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

随机推荐

  1. PHP利用P3P实现跨域

    本文转自:点这里 有别于JS跨域.IFRAME跨域等的常用处理办法,还可以利用P3P来实现跨域. P3P是什么 P3P(Platform for Privacy Preferences)是W3C公布的 ...

  2. loopback文档翻译

    最近在学习loopback,期间在strongloop的官网翻译了部分文章. 见:https://docs.strongloop.com/pages/viewpage.action?pageId=60 ...

  3. [译]git log进阶

    格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge bra ...

  4. MySQL Cluster 7.3.5 集群配置实例(入门篇)

    一.环境说明: CentOS6.3(32位) + MySQL Cluster 7.3.5,规划5台机器,资料如下: 节点分布情况: MGM:192.168.137. NDBD1:192.168.137 ...

  5. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  6. 虚拟机ping不通主机,但是主机可以ping通虚拟机

    http://chris2013.blog.51cto.com/6931081/1209278

  7. 守护神 Supervisor

    参考: http://linbo.github.io/2013/04/04/supervisor/ http://www.restran.net/2015/10/04/supervisord-tuto ...

  8. Unity 3D 我来了

  9. Python之路,Day4 - Python基础4

    一.函数 (一)背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警,你掏空了所有的知识量,写出了以下代码 1 2 3 4 ...

  10. nginx+webpy配置

    之前搞app时候学的webpy,一直用的自带webserver,最近研究nginx一段时间,决定二者结合玩一下~ 把搭建的要点总结下,说不定哪天还得用——其实平时手挺懒的... 1 必备模块和背景知识 ...