LeetCode 71. Simplify Path
来源:互联网 发布:家庭式音响推荐 知乎 编辑:程序博客网 时间:2024/06/01 08:32
以'/'切分路径,用vector<string> ret保存每一级的文件名;
当前文件名为"."时,忽略;
当前文件名为".."时,若未到达根目录,则ret.pop_back(), 反之忽略;
其他情况都ret.push_back( filename ).
代码:
class Solution {public: string simplifyPath(string path) { vector<string> dir; string ret; dir.push_back("/"); for (size_t begin, end=0; ; ) { for (begin=end+1 ; begin<path.size() && path[begin]=='/'; ++ begin) {} if (begin >= path.size()) { break; } end = path.find('/', begin); end = end!=string::npos? end: path.size(); auto cur = path.substr(begin, end-begin); if (cur!="." && cur!="..") { dir.push_back(cur + "/"); } else if (cur==".." && dir.size()>1) { dir.pop_back(); } } for (size_t i = 0; i < dir.size(); ++ i) { if (i==0 || i!=dir.size()-1) { ret += dir[i]; } else { ret += dir[i].substr(0, dir[i].size()-1); // no slash } } return ret; }};
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- [leetcode] 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode - 71. Simplify Path
- [LeetCode] 71. Simplify Path
- leetcode 71. Simplify Path
- 【环境配置】Linux的常用命令
- (华为机试)删除字符串中相同子串
- effecctive C++(一)
- java代理
- link
- LeetCode 71. Simplify Path
- win7安装Qt4.8.5 For Windows 最详细的教程,附带所有安装文件-- 转自zgj_today的csdn空间
- 重构方法
- HDOJ1001
- extjs环境搭建需要引入2个问题一个css一个js
- java学习之类-this,类变量,类方法
- java运行bat命令得到某个windows文件的创建时间
- android增量更新(打补丁方式)
- 【翻译自mos文章】尽管指定了目的地,asmcmd cp命令还是在 +DATA/ASM 下创建文件