71. Simplify Path
来源:互联网 发布:新手怎么做淘宝分销 编辑:程序博客网 时间:2024/05/29 19:07
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
只需要设置对/.. /. 和 / 的判断即可
public class Solution { public String simplifyPath(String path) { Stack<String> stack = new Stack<String>(); Set<String> set = new HashSet<String>(); set.add("."); set.add(".."); set.add(""); for(String s : path.split("/")){ if(s.equals("..")&& !stack.isEmpty()){ stack.pop(); }else if(!set.contains(s)) stack.push(s); } String res = ""; while(!stack.isEmpty()){ res = "/" + stack.pop() + res; } return res.isEmpty() ? "/" : res; }}
阅读全文
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
- LeetCode 383. Ransom Note
- bzoj 4916 神犇和蒟蒻
- CMAKE的常见命令合集
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Can
- 217. Contains Duplicate
- 71. Simplify Path
- java中的排序!
- ubuntu16.04+cuda8.0+opencv3.1+GTX1080配置caffe
- CSS盒子模型
- Password Management:Password in Configuration File(密码在配置文件中)解决方案
- pointer_5
- Android TextView 高亮超链接关键字
- 今日头条 CEO 张一鸣:面试了 2000 个年轻人,混得好的都有这 5 种特质
- arraylist