LEETCODE: Simplify Path
来源:互联网 发布:股票赢家软件 编辑:程序博客网 时间:2024/06/05 21:34
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
汗!不熟悉linux啊,'.'是当前目录,‘..’是到上一级目录。其他的都没有特殊含义。
class Solution {public: string simplifyPath(string path) { stack<string> words; path += '/'; string current; for(int ii = 0; ii < path.length(); ii ++) { if(path[ii] != '/') { current += path[ii]; } else { if(current.length() == 1 && current[0] == '.') { current.clear(); continue; } if(current.length() == 2 && current[0] == '.' && current[1] == '.') { if(!words.empty()) { words.pop(); } current.clear(); continue; } if(!current.empty()) { words.push(current); current.clear(); continue; } } } string result; while(!words.empty()) { result = '/' + result; result = words.top() + result; words.pop(); } if(!result.empty()) { result = result.substr(0, result.length() - 1); } result = "/" + result; return result; }};
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
- 禁忌搜索算法
- PHPEXCEL结合MYSQL+PHP实现数据库数据导出EXCEL实例
- unity学习之总结
- 简单讲一下C语言连接MySQL数据库
- 图深度优先搜索之能否到达终点
- LEETCODE: Simplify Path
- Java记录—swing应用
- [js学习笔记]PDF.js专题
- 汇总从代数角度与几何角度理解相似度计算方法(以机器学习Mahout框架为主线)
- SAX解析XML文档的步骤
- Metrics "Global Cache Average CR Get Time" is at 1.273(Unknown):from em 12c
- static in c++
- 【剑指Offer面试编程题】题目1371:最小的K个数--九度OJ
- 图深度优先搜索之block的个数