Simplify Path
来源:互联网 发布:淘宝店铺添加收藏 编辑:程序博客网 时间:2024/06/14 19:13
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”
思路:将路径根据“/”进行分割,得到去掉“/”的数组,然后将数组的内容放入栈中,当遇到“..”时,就把栈中的一个元素pop出来,当遇到“.”或者“”时,过滤到不添加到栈中。
最后遍历栈中元素便可得到简化后的路径了。
代码如下:
public String simplifyPath(String path) { Deque<String> stack = new LinkedList<>(); Set set = new HashSet<String>(); set.add(""); set.add("."); set.add(".."); for (String dir : path.split("/")) { if ("..".equals(dir) && !stack.isEmpty()) { stack.pop(); } if (!set.contains(dir)) { stack.push(dir); } } String result = ""; for (String dir : stack) { result = "/" + dir + result; } return result.isEmpty() ? "/" : 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
- Javascript原型继承 javascript
- 基于android的Socket通信
- BZOJ3730 震波 [点分治][点分序]
- 即使放弃你的代码,也不要放弃你的时间
- laravel 目录结构
- Simplify Path
- Session和Cookie的区别?
- 雷鸣的游戏人生(四) --- 营地趣事
- [工具设置]把项目放到码云上,通过git 进行项目管理
- 搜索相似之——BM25算法
- 数据库事务的四大特性以及事务的隔离级别
- Dubbo 启动 报 javassist/ClassPath And org/jboss/netty/channel/ChannelFactory异常
- fastcgi_finish_request
- MySQL数据库操作