71. Simplify Path
来源:互联网 发布:linux解压bin文件 编辑:程序博客网 时间:2024/05/18 18:53
这道题要求简化给定的路径。
我用了栈,用vector也可以,要注意有多个/连续出现的情况,所以要跳过多余的/,然后把/之间的路径名称提出来,如果是.,不做处理,如果是..,把栈最上面的弹出来。否则就入栈。遍历完了之后,如果栈为空,返回/即可,如果栈不为空,把路径输出来。注意有一个用例是/../,这个应该返回/
class Solution {public: string simplifyPath(string path) { stack<string> s; int start = 0; while(start < path.size()){ string part; while(start < path.size() && path[start] == '/') start++; if(start == path.size()) break; int end = start; while(end < path.size() && path[end] != '/') end++; end = end-1; part = path.substr(start, end - start + 1); if(part == ".."){ if(!s.empty()) s.pop(); }else if(part != "."){ s.push(part); } cout << part << endl; start = end + 1; } string res; if(s.empty()) res = "/"; while(!s.empty()){ res = "/" + s.top() + res; s.pop(); } return res; }};//测试用例/..///测试用例"/JD/nA/./tXa/./././DFaiU/l/..///../nPwm"
阅读全文
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71.Simplify Path
- 71.Simplify Path
- 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71. Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- leetcode 71. Simplify Path
- 机器学习_逻辑回归costFunction表示形式
- 算法刷题系列(1)leetcode349
- python数组拼接
- 数据结构栈的定义(java)
- CSS 浮动
- 71. Simplify Path
- radioButton的使用
- java
- 数字货币核心内容详细详解底层技术开发教程
- php rand 和 mt_rand区别
- 每天一道算法题——汉诺塔
- HTML布局
- 保存Bean到数据表的时候常遇见的问题
- Windows 10 标准版(中文版/核心版/家庭版)中开启远程桌面服务端