Simplify Path
来源:互联网 发布:数据库关系模式怎么写 编辑:程序博客网 时间:2024/05/14 05:00
使用一个栈来存储解析出来的每一个路径,遇到..则出栈。时间O(n),空间O(n)。
class Solution {public: string simplifyPath(string path) { vector<string> dirs; int start = 1; int cur = 0; while(cur = path.find('/', start)) { string dir; if(cur != string::npos) dir = path.substr(start, cur-start); else dir = path.substr(start, path.size()-start); if(dir == "..") { if(dirs.size())dirs.pop_back(); } else if(dir != "." && dir != "") dirs.push_back(dir); if(cur == string::npos) break; else start = cur+1; } //reconstruct the path from stack string result; if(dirs.empty()) return "/"; for(auto dir: dirs) { result += ("/" + dir); } return result; }};
0 0
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Netty 4.X 用户指南
- Memcached 是一个高性能的分布式内存对象缓存系统
- Matlab实例学习-----Matlab与Excel交互,Matlab作为自动化客户端调用Excel服务器
- 机器学习实战
- 自由
- Simplify Path
- easy-ui $.messager.show() msg是一些标签,并且有关闭的事件
- java 生成图片验证码
- 作为程序员创业者,来谈一下“老罗与王自如对战”
- C语言之野指针产生原因、易错模型、解决办法
- 解决ORACLE账号system被锁和修改密码
- CT值计算公式
- TIJ学习笔记之LinkedList
- 动态添加ImageView 设置setPadding不起作用问题