[Leetcode] Simplify Path
2012-12-17 02:44
316 查看
class Solution { public: string simplifyPath(string path) { // Start typing your C/C++ solution below // DO NOT write int main() function stack<string> stk; string str = ""; for (int i = 0; i < path.size(); ++i) { if (path[i] == '/') { if (str == "..") { if (!stk.empty()) stk.pop(); } else if (str != "." && str != "") { stk.push(str); } str = ""; } else { str += path[i]; } } if (str == "..") { if (!stk.empty()) stk.pop(); } else if (str != "." && str != "") { stk.push(str); } if (stk.empty()) return "/"; string res = ""; while (!stk.empty()) { res = "/" + stk.top() + res; stk.pop(); } return res; } };
相关文章推荐
- leetcode-Simplify Path
- LeetCode---Simplify Path
- [LeetCode]Simplify Path
- [LeetCode]Simplify Path
- LeetCode - Refresh - Simplify Path
- 【leetcode】Simplify Path
- [LeetCode]Simplify Path
- Leetcode Simplify Path
- 【LeetCode】Simplify Path 解题报告
- LeetCode 71. Simplify Path
- leetcode[71]Simplify Path
- Leetcode: Simplify Path
- Leetcode 71 Simplify Path
- 算法分析与设计课程(10):【leetcode】Simplify Path
- leetcode-Simplify Path
- LeetCode Online Judge 题目C# 练习 - Simplify Path
- Leetcode: Simplify Path
- LeetCode 71 Simplify Path (栈)
- LeetCode 071 Simplify Path
- leetcode 100: Simplify Path