71. Simplify Path**
来源:互联网 发布:与男闺蜜滚床单 知乎 编辑:程序博客网 时间:2024/05/21 14:00
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"
.
public class Solution { public 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 --- 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
- 第五章-存储器管理
- 面试习题整理(一)
- 依赖注入利器 - Dagger ‡
- 驱动复习2(mor8)
- 关于Redis---数据类型-String
- 71. Simplify Path**
- 专题1:链接器脚本
- 关于KMP算法中前缀函数(next函数)的一点个人理解
- C语言——用联合体判断大小端
- Android-Dalvik指令集
- 【HDU 5996 dingyeye loves stone】+ 尼姆博弈
- 业内
- 安装Subversion SVN
- 字符串 与 数组