71. Simplify Path
来源:互联网 发布:51单片机发展 编辑:程序博客网 时间:2024/06/02 04:34
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".
此题刚开始看着比较懵逼,不知道怎么求解,但是慢慢想想其实还是很简单的,可以忽略 ./ (代表当前目录),遇到 ../ 代表上一层目录,可以在对目录遍历的过程中不断的保存当前合法的路径,然后遇到 ../ 将当前路径弹出一个文件夹即可。
string simplifyPath(string path) { vector<string> res; for (auto it = path.begin(); it != path.end();){ it++; auto j = find(it, path.end(), '/'); string str = string(it, j); if (!str.empty() && str != "."){ if (str == ".."){ if (!res.empty())res.pop_back(); } else res.push_back(str); } it = j; } ostringstream out; if (res.empty())out << '/'; else for (auto x : res)out << '/' << x; return out.str(); }
阅读全文
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71.Simplify Path
- 71.Simplify Path
- 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71. Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- leetcode 71. Simplify Path
- 开始学习Android Studio
- Android Fragment 真正的完全解析(下)
- mfc acvtiveX注册(regsvr32)失败(找不到、版本不兼容)的解决办法
- 临时
- STM32进入HardFault_Handler的调试方法
- 71. Simplify Path
- python集成开发环境IDE搭建
- IMWeb提升营 —Day5(补)
- android通过webview调起支付宝app支付
- 按enter键自动刷新页面
- Activiti流程行为类
- 混淆矩阵(Confusion Matrix)分析
- SAP Query 导入、导出
- struts上传文件,报错:java.io.FileNotFoundException:(系统找不到指定的路劲)