Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

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;
}
};

最新文章

  1. #pg学习#postgresql的安装
  2. Oozie分布式任务的工作流——Sqoop篇
  3. 零配置简单搭建SpringMVC 项目
  4. 使用mybatis访问sql server
  5. [JS] JavaScript框架(2) D3
  6. AIX配置时间服务器(NTP)
  7. 2016 Multi-University Training Contest 1 Chess 组合游戏+状压(预处理)
  8. IOS 录像软件
  9. MVC中,加入的一个aspx页面用到AspNetPager控件处理办法
  10. poi读写Excel文件
  11. C语言库函数大全及应用实例八
  12. UWP 分享用那个图标
  13. 【原】javascript笔记之Array方法forEach&amp;map&amp;filter&amp;some&amp;every&amp;reduce&amp;reduceRight
  14. SmartBinding与kbmMW#1
  15. MySQL高可用架构-MMM环境部署记录
  16. asp.net 逻辑操作符与(&amp;&amp;),或(||),非(!)
  17. opencv2函数学习之threshold:实现图像阈值化
  18. aop 例外通知就是记录业务方法出现错误 并保存到日志里面的功能
  19. J2ee高并发情况下监听器
  20. overflow: scroll

热门文章

  1. Django Models的数据类型
  2. OGNL调用静态方法和属性
  3. linux留下后门的技巧
  4. Java开发中经典的小实例-(while(参数){})
  5. ubuntu14.04换一个更快的源
  6. ubuntu1604安装体验
  7. 用一张图片制作skybox图片 (如何制作360全景图、立方体)
  8. R 语言的优劣势是什么?
  9. mysql概要(八)视图
  10. chrome离线安装包_下载