Simplify Path [LeetCode]
2024-08-25 13:33:14
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Corner Cases:
- 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;
}
};
最新文章
- #pg学习#postgresql的安装
- Oozie分布式任务的工作流——Sqoop篇
- 零配置简单搭建SpringMVC 项目
- 使用mybatis访问sql server
- [JS] JavaScript框架(2) D3
- AIX配置时间服务器(NTP)
- 2016 Multi-University Training Contest 1 Chess 组合游戏+状压(预处理)
- IOS 录像软件
- MVC中,加入的一个aspx页面用到AspNetPager控件处理办法
- poi读写Excel文件
- C语言库函数大全及应用实例八
- UWP 分享用那个图标
- 【原】javascript笔记之Array方法forEach&;map&;filter&;some&;every&;reduce&;reduceRight
- SmartBinding与kbmMW#1
- MySQL高可用架构-MMM环境部署记录
- asp.net 逻辑操作符与(&;&;),或(||),非(!)
- opencv2函数学习之threshold:实现图像阈值化
- aop 例外通知就是记录业务方法出现错误 并保存到日志里面的功能
- J2ee高并发情况下监听器
- overflow: scroll