LeetCode-Simplify Path
来源:互联网 发布:matlab合并三个矩阵 编辑:程序博客网 时间:2024/05/16 10:54
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:
Solution:- 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"
.
Code:
<span style="font-size:14px;">class Solution {public: string simplifyPath(string path) { path += "/"; int length = path.size(); stack<string> stk; string name; for (int i = 0; i < length; i++) { if (path[i] == '/') { if (name == "." || name == "") { } else if (name == "..") { if (!stk.empty()) stk.pop(); } else stk.push(name); name = ""; } else name += path[i]; } string result; while (!stk.empty()) { result = "/" + stk.top() + result; stk.pop(); } if (result == "") return "/"; return result; }};</span>
0 0
- 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 simplify path
- Leetcode: Simplify Path
- LeetCode题解:Simplify Path
- [LeetCode] Simplify Path
- LeetCode Simplify Path
- HDU 1203 I NEED A OFFER! 01背包 dp
- Socket 构建和解析协议消息-- "投票"示例程序
- Java 垃圾收集与内存分配
- Linux命令:traceroute
- Linux用户及用户组设置
- LeetCode-Simplify Path
- hdu fast food(1227)
- BZOJ 1083: [SCOI2005]繁忙的都市
- 对数组中的自定义的类型根据姓名,年龄排序(升序, 降序)sortedArrayUsingSelector
- OllyDbg 使用笔记 (六)
- 算法复习--快速排序
- Linux命令:netstate
- mysql内连接更新语句
- windows7 下安装 redhat 双系统 easybcd