[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 ...
随机推荐
- cocos 帧率测试
有人说导致cocos2dx 帧率下降的是getPosition,我测试以后发现并不是这样的. local MainScene = class("MainScene", functi ...
- BZOJ3829 : [Poi2014]FarmCraft
d[x]表示走完x的子树并回到x所需的时间 f[x]表示从走到x开始计时,x子树中最晚的点安装完的最早时间 d[x]=sum(d[i]+2),i是x的孩子 f[x]的计算比较复杂: 考虑将x的各棵子树 ...
- IE6/IE7下绝对定位position:absolute和margin的冲突问题解决
首先我们来看一个代码: 复制代码代码如下:<div id=”layer1″ style=”margin:20px; border:1px solid #F88; width:400px; “&g ...
- HDU 4405 期望DP
期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...
- Java多线程编程详解
转自:http://programming.iteye.com/blog/158568 线程的同步 由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Ja ...
- 手把手教你 用 wpf 制作metro ProgressRing (Windows8 等待动画)
效果图: 还在羡慕metro的ProgressRing吗? wpf 也可以拥有 首先说下思路, 一共6个点围绕一直圆转,所以需要使用rotation动画 并且一直转下去. 那么下面的问题就好解决了. ...
- TYVJ P1036 统计数字 Label:坑!!!(用queue+map做出来的水)
背景 NOIP2007年提高组第一题 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数 ...
- TYVJ P1046 Blast Label:dp
描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为“abcbcd”,则字符串“abcb□cd”,“□a□bcbcd□”和“abcb□cd□”都是X的扩 ...
- VTK 6 和 VTK 5 的不同
Overview Replacement of SetInput() with SetInputData() and SetInputConnection() Removal of GetProduc ...
- unserialize函数中的参数是否是污染数据
1.原理 在程序编写的时候,往往需要序列化一些运行时数据,所谓序列化就是按照一定的格式将运行时数据写入本地文件.这样做可以对数据进行本地保存,用的时候直接读文件就可以把运行时产生的数据读出.php中就 ...