Leetcode NO.71 Simplify Path
来源:互联网 发布:无印良品水乳 知乎 编辑:程序博客网 时间:2024/06/05 18:22
本题题目于要求如下:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Corner Cases:
本题的难度并不高,对我来说,难度主要集中在unix-style command的含义。。后来搞明白了:- 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"
.
- "."的意思是忽略,不做任何处理。
- ".."的意思是返回上一级,具体在本题的stack的操作是,pop栈顶的元素,由于栈顶的下一层即为上一级的路径。。
具体代码如下:
class Solution {public: string simplifyPath(string path) { stack<string> res_stack; string res = ""; string token; istringstream is(path); while (getline(is, token, '/')) { if (token == "." or token == "") { continue; } else if (token == ".." and !res_stack.empty()) { res_stack.pop(); } else if (token != "..") { res_stack.push(token); } } while (!res_stack.empty()) { res = "/" + res_stack.top() + res; res_stack.pop(); } if (res == "") { res = "/"; } return res; }};
0 0
- Leetcode NO.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
- LeetCode 71 Simplify Path
- Leetcode 71 Simplify Path
- leetcode-71 simplify path
- LeetCode #71 Simplify Path
- [leetcode-71]Simplify Path(C)
- LeetCode(71) Simplify Path
- LeetCode 71 Simplify Path (栈)
- 【LeetCode】C# 71、Simplify Path
- 【Leetcode】71 Simplify path 简化路径
- 147Insertion Sort List
- Operating System -- Process Description And Control
- 安卓手机安装gcc,编译c/c++
- Java多线程及其使用(一)
- Bash 11-13
- Leetcode NO.71 Simplify Path
- iOS 在UILabel显示不同的字体和颜色(转) addAttribute
- java 小项目:简单扑克牌游戏
- win7平台下QT软件的打包与发布 (利用NSIS脚本制作安装包)
- Magento迁移服务器
- C#.ToString()格式大全
- weka实战005:基于HashSet实现的apriori关联规则算法
- codeforces 510c Fox And Names 拓扑排序
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)