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. HDU1116 Play on Words(有向图欧拉通路)

    我把单词当作点,然后这样其实是不对的,这样就要判定是否是哈密顿通路.. 这题应该把单词的首尾单词当作点,而单词本身就是边,那样就是判定欧拉通路了. 有向图包含欧拉通路的充要条件是:首先基图连通,然后是 ...

  2. Transactional topologies —— 事务拓扑

    事务拓扑是怎么回事? Storm guarantees that every message will be played through the topology at least once. St ...

  3. 【BZOJ】1067: [SCOI2007]降雨量(rmq+变态题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1067 好不爽,弄了一个晚上. 好不爽. 还是照着别人程序拍着看的!!! 噗 这题很变态. 首先,我没 ...

  4. POJ 3140 Contestants Division

    题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...

  5. 解决EasyUI-Datagrid和LinqToEntity结合应用时排序问题

    我们在做WEB页面时,时常会选择JQuery框架的Datagrid,如Ext.EasyUI.Flexigrid,数据访问则采用LinqToSQL或LinqToEntity.UI用Jquery框架的目的 ...

  6. [转]Redis集群的配置

    一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [redis 特点] 1:速度没有m ...

  7. Swift Internal Parameter and External Parameter 外部参数和内部参数

    今天跟大神又学习了些关于IOS开发Swift语言的外部参数和内部参数 func doSomething(num1: Int, num2: Int) -> Int { return num1 + ...

  8. 通过JDBC连接hive

    hive是大数据技术簇中进行数据仓库应用的基础组件,是其它类似数据仓库应用的对比基准.基础的数据操作我们可以通过脚本方式以hive-client进行处理.若需要开发应用程序,则需要使用hive的jdb ...

  9. hdu 3348 coins

    这道题算是一道很经典的题,很好的诠释了贪心和动态规划的不同功能.求最少钱的数量用贪心就够了,但是求最多钱的数量要用到动态规划的思想,每步都尽量保留最大 数量.具体看程序注解: #include&quo ...

  10. 新建childTest文件夹,里面依然放进我们需要的.py文件即可

    一.模块 我们编写文件:a.py,放在C:\Python34\Lib\sit-packages下,里面写上一句代码为: print('this is a') 之后我们就可以在我们的代码里面引用a.py ...