leetcode题解-71. Simplify Path
来源:互联网 发布:微信网络诈骗 编辑:程序博客网 时间:2024/05/19 23:28
题意:对于给定的类Unix系统的文件路径,对其进行简化。
分析:在Unix系统中”.”表示当前路径,”..”表示上一级路径。
这道题要使用一个栈,来保存当前的路径状况。我们首先需要按照“/”将地址进行分割,分割出不同的操作:
1、如果是空或者“.”,那么当前没有什么地址变动,地址栈不需要变动 。
2、如果是“..” 则需要出栈(如果栈为空则不操作)因为这是返回上级目录的符号 。
3、其他情况压栈。
class Solution { public static String simplifyPath(String path) { Deque<String> stack = new LinkedList<>(); Set<String> skip = new HashSet<>(Arrays.asList("..",".","")); for (String dir : path.split("/")) { if (dir.equals("..") && !stack.isEmpty()) stack.pop(); else if (!skip.contains(dir)) stack.push(dir); } String res = ""; for (String dir : stack) res = "/" + dir + res; return res.isEmpty() ? "/" : res; }}
阅读全文
0 0
- leetcode题解-71. Simplify Path
- LeetCode题解:Simplify Path
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- [leetcode] 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode - 71. Simplify Path
- springboot添加https
- CCPC.2017哈尔滨站-重现赛-A(manacher+树状数组)
- MFC按钮美化
- 古希腊
- NodeJS on Nginx: 使用nginx反向代理处理静态页面
- leetcode题解-71. Simplify Path
- linux基础学习7 软件安装
- 软连接和硬链接
- VS实现获取图像灰度共生矩阵
- UVA 11806 Cheerleaders——计数原理
- 小结一
- 5.Java的四种引用,强弱软虚,用到的场景。
- 【C++对象模型】之Data Members
- 欢迎使用CSDN-markdown编辑器