LeetCode OJ Simplify Path
来源:互联网 发布:淘宝怎么卖二手闲置 编辑:程序博客网 时间:2024/06/03 21:19
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"
.
class Solution {public: string simplifyPath(string path) { vector<string> fileName; for (int i = 0; i < path.size(); i++) { if (path[i] == '/') { if (i < path.size() - 1 && path[i + 1] == '/') { // situation for "//" i += 1 - 1; continue; } if ((i == path.size() - 2 && path[i + 1] == '.') || (i < path.size() - 2 && path[i + 1] == '.' && path[i + 2] == '/')) { // situation for "/." i += 2 - 1; continue; } if ((i == path.size() - 3 && path[i + 1] == '.' && path[i + 2] == '.') || (i < path.size() - 3 && path[i + 1] == '.' && path[i + 2] == '.' && path[i + 3] == '/')) { // situation for "/.." i += 3 - 1; if (fileName.size() > 0) { fileName.pop_back(); } continue; } string name; // if the situation is not above, it is a valid file name int j = i + 1; for (; j < path.size() && path[j] != '/'; j++) { name.push_back(path[j]); } if (name.size() != 0) fileName.push_back(name); i = j - 1; continue; } } string ans; for (int i = 0; i < fileName.size(); i++) { ans += "/" + fileName[i]; } if (ans.size() == 0) ans = "/"; return ans; }};
0 0
- LeetCode OJ:Simplify Path
- LeetCode OJ - Simplify Path*
- LeetCode OJ Simplify Path
- LeetCode OJ算法题(七十一):Simplify Path
- LeetCode OJ 之 Simplify Path (简化路径)
- LeetCode: Simplify Path
- LeetCode Simplify Path
- LeetCode : Simplify Path
- [Leetcode] Simplify Path
- [leetcode] simplify path
- [LeetCode] Simplify Path
- leetcode 100: Simplify Path
- [leetcode]Simplify Path
- [LeetCode]Simplify Path
- [leetCode] Simplify Path
- [leetcode]Simplify Path
- [leetcode] Simplify Path
- [LeetCode] Simplify Path
- LeetCode OJ Binary Tree Level Order Traversal
- LeetCode OJ Binary Tree Level Order Traversal II
- LeetCode OJ Climbing Stairs
- 10 个非常有用的 SVG 动画的 JavaScript 库
- Java误区之判断奇数
- LeetCode OJ Simplify Path
- LeetCode OJ Search in Rotated Sorted Array II
- JAVA设计模式(二十二)——命令行模式
- LeetCode OJ Remove Duplicates from Sorted Array II
- Java与C++的微妙
- LeetCode OJ Longest Consecutive Sequence
- LeetCode OJ Balanced Binary Tree
- 527B. Error Correct System【string】
- 数控系统_01