Simplify Path [LeetCode]
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"
.
Summary: Very careful about corner cases, like "/", "/.", "..".
class Solution {
public:
void handleEntry(vector<string> &path_stack, string path_entry) {
if(path_entry == "..") {
if(path_stack.size() > )
path_stack.pop_back();
}else if (path_entry != ".")
path_stack.push_back(path_entry);
} string simplifyPath(string path) {
string simple_path;
if(path.size() == )
return simple_path;
vector<string> path_stack;
string path_entry;
for(int i = ; i < path.size(); i ++) {
if(path_entry.size() == && path[i] == '/'){
continue;
}else if (path_entry.size() != && path[i] == '/') {
handleEntry(path_stack, path_entry);
path_entry.clear();
}else {
path_entry.push_back(path[i]);
}
}
if(path_entry.size() > )
handleEntry(path_stack, path_entry); for(string item: path_stack) {
simple_path += "/";
simple_path += item;
}
if(simple_path.size() == )
return "/";
return simple_path;
}
};
Simplify Path [LeetCode]的更多相关文章
- Simplify Path——LeetCode
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Simplify Path leetcode java
题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&qu ...
- 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 ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- linux bash shell 流程控制(if/else )
本文转自:http://blog.csdn.net/flowingflying/article/details/5069646 本文也即<Learning the bash Shell>3 ...
- HQL语句大全(转载)
Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承.多态 和关联之类的概念. ...
- 自动编号维护SNRO
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 禁止ubuntu的super快捷键
在mac上安装了ubuntu虚拟机, 但是发现command健(ubuntu中叫super健)被系统占用了, 习惯了command健的同学来说非常不方便, 如何禁用默认的command健呢? You ...
- Git学习(2)Git 安装
Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行: 安装包下载地址 ...
- Mysql delete,truncate,drop
1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...
- Javascript设计模式之创建对象的灵活性
传统的 /* Anim class */ var Anim = function () {}; Anim.prototype.start = function () { console.log(&qu ...
- linux清空文件等有用的指令
1). > filename 2). :> filename 3). echo "" > filename (文件大小被截为1字节) 4). ...
- Windows的命令行查看,修改,删除,添加环境变量
查看当前所有可用的环境变量:输入 set 即可查看. 显示某环境变量: 例如:echo %SystemDrive% 类似linux下面的echo $PATH这种用法. 首先明确一点:所有的在cmd命令 ...
- gcc, numpy, rabbitmq等安装升级总结
1. 公司在下面目录安装了gcc-4.8.2,以支持c++11,可以通过在bashrc中添加来实现: PATH=/opt/compiler/gcc-4.8.2/bin:$PATH 2. 公司环境切换到 ...