[LeetCode] Simplify Path(可以不用看)
Given an absolute path for a file (Unix-style), simplify it.
For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"
- 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(可以不用看)的更多相关文章
- [LeetCode] Simplify Path,文件路径简化,用栈来做
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- Leetcode Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- POJ1797 Heavy Transportation(SPFA)
题目要求1到n点的最大容量的增广路. 听说是最短路求的,然后乱搞就A了.. 大概能从Bellman-Ford的思想,dk[u]表示从源点出发经过最多k条边到达u点的最短路,上理解正确性. #inclu ...
- django 代码片段3
#coding=utf-8 # 没有++ 和--其他的都一样 foo=1 show_output=True if show_output and foo==1: print 'pyhont and % ...
- BZOJ4295 : [PA2015]Hazard
第i轮,a[i%n]+=b[i%m]. 枚举i,计算它变为0的次数,假设为t,那么有t=i+kn. 对于所有的i和k,(i+kn)%m形成了若干个总长度为m的环. 对于每个a[i],先在环中求出一轮最 ...
- BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)
将每个串正着插入Trie A中,倒着插入Trie B中. 并求出每个串在A,B中的dfs序. 每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席 ...
- slice、substring和substr
首先,他们都接收两个参数,slice和substring接收的是起始位置和结束位置(不包括结束位置),而substr接收的则是起始位置和所要返回的字符串长度.直接看下面例子: 1 var tes ...
- shell字符串和数组
字符串 : 1.单引号: str = 'value' 单引号字符串的限制: 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单引号(对单引号使用转义符后也不行); ...
- Idea_编译报错 javacTask: 源发行版 1.6 需要目标发行版 1.6
在idea中编译时发生如下的错误 Information:Using javac 1.7.0_75 to compile java sources Information:java: javacTas ...
- CentoS 下报的 Requires: perl(:MODULE_COMPAT_5.8.8)
yum error requires: libtcmalloc.so.4 rpm -Uvh http://ceph.com/rpm-cuttlefish/el6/x86_64/ceph-release ...
- swift -- 学习记录
先把疯狂的swift这本书大致看了一遍 2016.7.13 因为实在是太闲,所以决定把公司的应用用swift写一遍 然后顺便看看swift的官方文档 这里有一个官文的中文翻译,感动啊 http://w ...
- 全屏滚动-jQuery插件实现
全屏滚动 <---很久没写了,这段忙了点,以后还是每周尽量写点东西---> 在很多情况下,我们需要页面的全屏滚动,尤其是移动端.今天简要的介绍一下全屏滚动的知识. 一.全屏滚动的原理 1. ...