LeetCode---Simplify Path
来源:互联网 发布:全球经济数据时时播报 编辑:程序博客网 时间:2024/05/28 15:07
题目大意:给出一个UNIX-style的路径,将其进行简化,如果遇见“/.”则保持当前路径 ,如果遇见“/..”则返回上一层路径,最后输出简化后的路径。
算法思想:
1.将给出的路径进行分割将每一层路径存储在容器中。
2.按照简化规则进行路径简化,遍历容器如果遇见“.”则continue,如果遇见“..”则进行出栈操作(栈不空),如果是其他字符串则入栈。
3.将栈中存储的简化后的路径单元拼接起来。
4.返回简化后的路径。
代码如下:
class Solution {public: string simplifyPath(string path) { if(path.size()<=1) return "/"; vector<string> str; stack<string> str_pat; string temp="",res=""; for(char c:path){ if(c!='/') temp+=c; else{ if(temp.size()!=0){ str.push_back(temp); temp=""; } } } if(temp.size()!=0) str.push_back(temp); for(string s:str){ if(s=="."){ continue; } else if(s==".."){ if(!str_pat.empty()) str_pat.pop(); } else{ str_pat.push(s); } } while(!str_pat.empty()){ temp="/"+str_pat.top()+res; str_pat.pop(); res=temp; temp=""; } if(res.size()==0) res="/"; return res; }};
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
- return *this和return this有什么区别?
- cenos7.1 安装openstack kilo 心得之三添加网络时间协议
- phpstorm常用快捷键
- 杭电1072 猜数字
- 工业4.0时代已经到来,你准备好了吗?
- LeetCode---Simplify Path
- Modules 'libxmlSimu' not found 在使用XMPP时导入KissXML时出现的问题
- 创建型设计模式:简单工厂模式--分析优缺点
- struct和typedef struct彻底明白了
- 指针作为函数参数传递
- 多线程编程之二——MFC中的多线程开发
- 九度OJ 1261:寻找峰值点 (基础题)
- 从大数据菜鸟走上大师的历程
- HDU 2151 Worm(和树塔问题差不多)