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"
.
被这一串的文件路径名给唬住了,还是缺乏分析问题的能力啊,path = "/a/./b/../../c/"
, => "/c"其实就是当前目录和上层目录相互抵消啊,从左往右依次看过去就行了,然后就是以“/”为分割符分割这个字符串了.
以下转自:http://blog.csdn.net/makuiyu/article/details/44497901
字符串处理,由于".."是返回上级目录(如果是根目录则不处理),因此可以考虑用栈记录路径名,以便于处理。需要注意几个细节:
- 重复连续出现的'/',只按1个处理,即跳过重复连续出现的'/';
- 如果路径名是".",则不处理;
- 如果路径名是"..",则需要弹栈,如果栈为空,则不做处理;
- 如果路径名为其他字符串,入栈。
最后,再逐个取出栈中元素(即已保存的路径名),用'/'分隔并连接起来,不过要注意顺序呦。
时间复杂度:O(n)
空间复杂度:O(n)
class Solution
{
public:
string simplifyPath(string path)
{
stack<string> ss; // 记录路径名
for(int i = ; i < path.size(); )
{
// 跳过斜线'/'
while(i < path.size() && '/' == path[i])
++ i;
// 记录路径名
string s = "";
while(i < path.size() && path[i] != '/')
s += path[i ++];
// 如果是".."则需要弹栈,否则入栈
if(".." == s && !ss.empty())
ss.pop();
else if(s != "" && s != "." && s != "..")
ss.push(s);
}
// 如果栈为空,说明为根目录,只有斜线'/'
if(ss.empty())
return "/";
// 逐个连接栈里的路径名
string s = "";
while(!ss.empty())
{
s = "/" + ss.top() + s;
ss.pop();
}
return s;
}
};
Simplify Path——简单经典的预处理的更多相关文章
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [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 ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- FOR XML PATH 简单介绍
FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- LeetCode OJ: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/", ...
随机推荐
- 基于DCT系数的实时监控中运动目标检测
本文的主要内容来自2009 Advanced Video and Signal Based Surveillance会议的一篇论文“Real-Time Moving Object Detection ...
- Python图像处理库(PIL)
官方:(详细)http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html http://pillow.readthedocs.io/e ...
- ReentrantLock和synchronized区别和联系?
相同:ReentrantLock提供了synchronized类似的功能和内存语义,都是可重入锁. 不同: 1.ReentrantLock功能性方面更全面,比如时间锁等候,可中断锁等候,锁投票等,因此 ...
- 007.C++构造函数
1.一个引例 //class head class complex //class body {} { public: complex(double r=0, double i) :re(r), im ...
- POJ 2112 二分+最大流
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 17297 Accepted: 6203 ...
- HDU1507二分图
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- JavaScript知识递归实现数组中指定后代元素的查找
描述:要求将下面的数据类型中每个子孙后代id放入一个数组并返回 var arr = [ {"id":28,"text":"公司信息", &q ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- BZOJ2733:使用并查集维护连通性之后用线段树维护+线段树合并(动态开点)
可以说是线段树合并的裸题吧 题意就是给你两个操作 一个操作是合并两个集合,这两个集合都是用权值线段树维护的,便于查询第k小元素 另一个操作就是查询区间极值了 #include<cstdio> ...
- PowerDesigner16 状态图
状态图(Statechart Diagram)主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列,引起状态转移的事件(Event),以及因状态转移而伴随的动作(Action). ...