leetcode---Simplify Path---栈
2016-05-27 22:40
344 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”
For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”
class Solution { public: string simplifyPath(string path) { stack<string> s; string ans = ""; int i = 0; while(i < path.length()) { while(i<path.length() && path[i] == '/') i++; string name = ""; while(i<path.length() && path[i] != '/') name += path[i++]; if(name == ".." && !s.empty()) s.pop(); else if(name != "" && name != "." && name != "..") s.push(name); } if(s.empty()) ans = "/"; else { while(!s.empty()) { ans = "/" + s.top() + ans; s.pop(); } } return ans; } };
相关文章推荐
- 谈谈final、finally、finalize的区别
- Markdown笔记
- Servlet/JSP-05 Cookie
- n皇后问题
- 算法学习与代码实现3——合并排序
- java jni 开发
- 实现自己的printf函数(1)
- Ubuntu14.04下Git安装和使用快速入门
- python_day3学习笔记
- bootStrap树形目录组件
- 关于fragment
- 直接选择排序
- 文本处理工具sed编辑器
- <c:forEach>详解
- 不多于5位数的正整数1、求出它是几位数;2、按逆序打印出各位数字。
- php 编程笔记分享 - 非常实用
- php 编程笔记分享 - 非常实用
- 相对准确的java程序性能测试实现方案
- 基因数据处理28之avocado运行
- Linux内核编程五:内存管理