[leetcode] Simplify Path
2015-07-01 17:36
357 查看
From : https://leetcode.com/problems/simplify-path/
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
click to show corner cases.
Corner Cases:
Did you consider the case where path =
In this case, you should return
Another corner case is the path might contain multiple slashes
such as
In this case, you should ignore redundant slashes and return
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
"/home/", =>
"/home"
path =
"/a/./b/../../c/", =>
"/c"
click to show corner cases.
Corner Cases:
Did you consider the case where path =
"/../"?
In this case, you should return
"/".
Another corner case is the path might contain multiple slashes
'/'together,
such as
"/home//foo/".
In this case, you should ignore redundant slashes and return
"/home/foo".
class Solution { public: string simplifyPath(string path) { if(path == "") return ""; stack<string> box; for(int i=0, sz=path.size(); i<sz; i++) { if(path[i] == '/') continue; string s; while(i<sz && path[i] != '/') { s = s+path[i++]; } if(s == ".") continue; else if(s == "..") { if(!box.empty()) box.pop(); } else box.push(s); } string ans; while(!box.empty()) { ans = "/"+box.top() + ans; box.pop(); } if(ans != "") return ans; return "/"; } };
相关文章推荐
- 关键字驱动和数据驱动
- 用matlab检测斑马鱼重心
- mysql游标示例mysql游标简易教程
- 浅析过滤器
- 整合百度语音到图灵机器人
- 不停止MySQL服务增加从库的两种方式
- ANDROID NDK使用第三方静态库的方法
- nginx容错相关配置
- 解决客户 IE 浏览器"兼容性视图"设置带来的问题
- Access数据库转换成Sqlite数据库
- 高并发高效连接池设计
- ios8 中改变label的高度
- WinSCP 连接 Ubuntu 拒绝的问题
- 代码
- android r.styleable是什么或报错
- 事务总结
- (7.2.5.1)在执行此操作之前,请在数据库中创建一个主密钥或在会话中打开该主密钥。
- 无密码访问ssh
- 删除字符串中出现次数最少的字符
- 使用apktool解包和打包apk