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/", ...
随机推荐
- httpClient需要的jar包
- Codeforces Round #392 (Div. 2) A B C 水 模拟 暴力
A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standa ...
- ps命令查看进程指定项目信息、用户名过长显示UID
有次一个在使用ps命令时,发现部分用户显示的是用户名,有些用户显示的是UID,那是因为用户名长度超过8位的:也就是说ps命令用户名列默认只能显示8位(含8位)的用户名,超过8位就显示UID,如何让长度 ...
- idea 多模块引用
roma-server 引用common-utils的类,所以在roma-server 的pom中配置 <dependency> <groupId>org.springfram ...
- cin.get()、流和缓冲区
大家好,这是我在CSDN的第一篇博客.我是一名学习GIS专业的大学生.我从小开始喜欢编程,可是到现在编程水平却长进不大,依然是菜鸟一个.究其原因,虽然这些年乱七八糟的东西学过不少,但是总的来说还是基础 ...
- svn稀疏目录--通过设置工作目录的深度(depth)实现目录树的部分签出
对于一个大的版本库来说,本地工作目录签出整个目录树是即费时又占地儿的.虽然可以只签出某个子目录树,但有时候还是需要从根目录签出.那么,怎么才能只把自己感兴趣的子目录签出来呢? 从svn1.5版开始,提 ...
- mysql 联合索引匹配原则
读mysql文档有感 看了mysql关于索引的文档,网上有一些错误的博客文档,这里我自己记一下. 几个重要的概念 1.对于mysql来说,一条sql中,一个表无论其蕴含的索引有多少,但是有且只用一条. ...
- 【C++对象模型】第二章 构造函数语意学
1.Default Constructor 当编译器需要的时候,default constructor会被合成出来,只执行编译器所需要的任务(将members适当初始化). 1.1 带有 Defau ...
- jsp 内置对象二
1.什么是session ? (1)session 表示客户端与服务器的一次回话. 2)Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网 ...
- mysql 可视化界面操作指令
1.让自增长从新开始 ALTER TABLE users auto_increment =1;//让表中的自增长从新从0开始 2.条件查询 SELECT name from users WHERE ...