[LeetCode] Simplify Path 简化路径
2015-03-18 14:39
387 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
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
In this case, you should ignore redundant slashes and return
这道题让简化给定的路径,光根据题目中给的那一个例子还真不太好总结出规律,应该再加上两个例子 path =
解法一
还有一种解法是利用了C语言中的函数strtok来分隔字符串,但是需要把string和char*类型相互转换,转换方法请猛戳这里。除了这块不同,其余的思想和上面那种解法相同,代码如下:
解法二
LeetCode All in One 题目讲解汇总(持续更新中...)
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".
这道题让简化给定的路径,光根据题目中给的那一个例子还真不太好总结出规律,应该再加上两个例子 path =
"/a/./b/../c/", =>
"/a/c"和path =这样我们就可以知道中间是"."的情况直接去掉,是".."时删掉它上面挨着的一个路径,而下面的边界条件给的一些情况中可以得知,如果是空的话返回"/",如果有多个"/"只保留一个。那么我们可以把路径看做是由一个或多个"/"分割开的众多子字符串,把它们分别提取出来一一处理即可,代码如下:[/code]"/a/./b/c/", => [code]"/a/b/c",
解法一
class Solution { public: string simplifyPath(string path) { vector<string> v; int i = 0; while (i < path.size()) { while (path[i] == '/' && i < path.size()) ++i; if (i == path.size()) break; int start = i; while (path[i] != '/' && i < path.size()) ++i; int end = i - 1; string s = path.substr(start, end - start + 1); if (s == "..") { if (!v.empty()) v.pop_back(); } else if (s != ".") { v.push_back(s); } } if (v.empty()) return "/"; string res; for (int i = 0; i < v.size(); ++i) { res += '/' + v[i]; } return res; } };
还有一种解法是利用了C语言中的函数strtok来分隔字符串,但是需要把string和char*类型相互转换,转换方法请猛戳这里。除了这块不同,其余的思想和上面那种解法相同,代码如下:
解法二
class Solution { public: string simplifyPath(string path) { vector<string> v; char *cstr = new char[path.length() + 1]; strcpy(cstr, path.c_str()); char *pch = strtok(cstr, "/"); while (pch != NULL) { string p = string(pch); if (p == "..") { if (!v.empty()) v.pop_back(); } else if (p != ".") { v.push_back(p); } pch = strtok(NULL, "/"); } if (v.empty()) return "/"; string res; for (int i = 0; i < v.size(); ++i) { res += '/' + v[i]; } return res; } };
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- LeetCode-Simplify Path-路径简化-栈的应用
- 【Leetcode】71 Simplify path 简化路径
- [C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)
- [Leetcode] Simplify path 简化路径
- [LeetCode]—Simplify Path 简化路径表达式
- Simplify Path 简化文件路径@LeetCode
- [LeetCode] Simplify Path,文件路径简化,用栈来做
- leetcode【第十二周】简化路径
- *[Lintcode]Simplify Path简化路径
- LeetCode 71. Simplify Path(简化路径)
- [LintCode] Simplify Path 简化路径
- leetcode+linux路径简化,stringstream是个好东西
- Leet Code 71 Simplify Path - 简化路径 - Java
- 【九度】题目1090:路径打印 && 【LeetCode】Simplify Path
- Leetcode 71. Simplify Path 简化路径 解题报告
- [LeetCode] 71. Simplify Path 简化路径
- LeetCode 71. Simplify Path(路径简化)
- LeetCode OJ 之 Simplify Path (简化路径)
- LeetCode:简化路径(Simplify Path)
- LeetCode-----71. Simplify Path(简化文件路径)