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

注意以下几点:input"/.", output"/"

input"/..", output"/"

input"/...", output"/..."即“...”、“....”等是有效的文件名

class Solution {
public:
string simplifyPath(string path) {
if(path == "/" || path=="")
return path;
string result(,path[]);
int len = path.size();
if(path[len-]!='/'){
path.push_back('/');
len++;
} int j=,start;
stack<int> sstart;
for(int i = ;i<len;){
if(path[i] != '/' && path[i] != '.'){//if(1) while(i<len && path[i]!='/'){
result.push_back(path[i++]);
j++;
}
int flag = j;
while(flag>= && result[flag]!='/'){
flag--;
}
sstart.push(flag+);
if(i<len- && path[i]=='/'){
result.push_back(path[i++]);
j++;
}else if(i == len- && path[i]=='/'){
return result;
}
}else{
if(path[i]=='/' && result[j]=='/')
i++;
else if(i<len- && path[i]=='.' && path[i+]=='/'){
i=i+;
}else if(i<len- && path[i]=='.' && path[i+]=='.'&& path[i+]=='/'){
i = i+;
if(result.size() == )
continue;
else{
if(result[j]=='/'){
start = sstart.top();
sstart.pop();
result.erase(result.begin()+start,result.end());
j = start-;
}else{ // "/.../""output"/.../"
int flag = j;
while(flag>= && result[flag]!='/'){
flag--;
}
sstart.push(flag+);
result.push_back(path[i-]);
result.push_back(path[i-]);
if(i-<len- && path[i-]=='/'){
result.push_back(path[i-]);
j+=;
}else if(i- == len- && path[i-]=='/'){
return result;
}
}
}
}else{
result.push_back(path[i++]);
j++;
}
}//end if(1)
}//end for
while(result[j]=='/' && result.size()!=){
result = result.substr(,j);
j--;
}
return result;
}//end func
};

[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 简化路径

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

  3. [leetcode]Simplify Path @ Python

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

  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. BZOJ3836 : [Poi2014]Tourism

    对于一个连通块,取一个点进行dfs,得到一棵dfs搜索树,则这棵树的深度不超过10,且所有额外边都是前向边. 对于每个点x,设S为三进制状态,S第i位表示根到x路径上深度为i的点的状态: 0:选了 1 ...

  2. 十位一线专家分享Spark现状与未来----峰会摘录

    CSDN大数据技术: 十位一线专家分享Spark现状与未来(一) 十位一线专家分享Spark现状与未来(二) 十位一线专家分享Spark现状与未来(三) 部分摘录: 加州大学伯克利分校AMP实验室博士 ...

  3. POJ 1064 (二分)

    题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 ...

  4. HDU 4003 (树形DP+背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4003 题目大意:有K个机器人,走完树上的全部路径,每条路径有个消费.对于一个点,机器人可以出去再回来 ...

  5. BZOJ4026: dC Loves Number Theory

    Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯 竭的水题资源.    给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所 ...

  6. u盘文件系统故障的修复方法

    比如U盘挂载的文件系统是/dev/sda1,且文件系统有故障(FAT: Filesystem error) 修复U盘文件系统故障 sudo dosfsck -v -a /dev/sda1

  7. java面试题中常见的关于String类问题总结

    问题1: String s1 = “abc”;String s2 = “abc”;System.out.println(s1 == s2); 这里的结果是true. 由于字符串是常量(内存中创建对象后 ...

  8. Using pg_dump restore dump file on Odoo

    pg_restore -C -d postgres db.dump

  9. 如何在URL筛选管理器中过滤不需要的URL

    互联网可以说是一把名副其实的双刃剑.一方面其可以提高工作效率.给企业提供充分的资源;另一方面如果管理不严,也会带来很多的隐患.如员工在上班时间玩游戏.炒股等等.为此现在很多企业希望对员工的网络行为进行 ...

  10. IOS第六天(1:scrollView 属性和查看大图)

    ***查看大图 #import "HMViewController.h" @interface HMViewController () <UIScrollViewDelega ...