leetcode71. Simplify Path
来源:互联网 发布:上海大学网络选课 编辑:程序博客网 时间:2024/05/16 01:08
71. Simplify Path
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) { if (path == null || path.length() == 0) { return null; } Stack<String> stack = new Stack<>(); List<String> list = Arrays.asList("..", ".", ""); String[] dirs = path.split("/"); for (String dir : dirs) { if (dir.equals("..") && !stack.isEmpty()) { stack.pop(); } else if (!list.contains(dir)) { stack.push(dir); } } String res = ""; while (!stack.empty()) { res = "/" + stack.pop() + res; } return res.isEmpty() ? "/" : res; }}
0 0
- Simplify Path Leetcode71
- LeetCode71. Simplify Path
- Leetcode71: Simplify Path
- leetcode71. Simplify Path
- LeetCode71 Simplify Path
- LeetCode71 Simplify Path java题解
- LeetCode71——Simplify Path
- 算法设计与分析HW8:LeetCode71.Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- C++实验四,项目7,百钱百鸡问题
- Java Web项目中解决中文乱码方法总结
- 高斯混合模型GMM(Gaussian Mixture Model)
- 基于chirp信号的阵列信号处理研究随记
- linux后台启动进程并记录进程ID
- leetcode71. Simplify Path
- 内存泄露简单解决办法
- 作业4
- Flatpak 新技能—— Linux 独立应用
- 作业3
- 数据结构中链表
- Softmax回归介绍
- Chrome开发者工具不完全指南(一、基础功能篇)
- 【bzoj4008】[HNOI2015]亚瑟王