3.14 Simplify Path
来源:互联网 发布:淘宝dsr计算器 编辑:程序博客网 时间:2024/05/22 13:58
原题链接:https://oj.leetcode.com/problems/simplify-path/
这道题不难,但是我一开始不理解path = "/a/./b/../../c/"
, => "/c",
while"/home//foo/"
return "/home/foo"
.Shouldn't the latter return /foo?
我的想法:如果只用输出最后一层目录,那可以直接找到最后两个“/”, 然后输出中间的字符串。
这个想法是错误的,问题在于我没有弄懂Unix path的定义。"/a/./b/../../c/ => /c是因为../../向上找了两层目录。
public class Solution { //code after reading answer public String simplifyPath(String path) { ArrayList<String> list = new ArrayList<String>(); String[] arr = path.split("/"); for(int i = 0; i < arr.length; i++){ if(arr[i].equals("") || arr[i].equals(".")){ continue; } else if(arr[i].equals("..")){ if(list.size() > 0){ list.remove(list.size()-1); } } else{ list.add(arr[i]); } } StringBuffer sb = new StringBuffer(); for(String s : list){ sb.append("/"); sb.append(s); } //return sb.isEmpty()? null : sb.toString(); return sb.length() ==0 ? "/" : sb.toString(); }}
0 0
- 3.14 Simplify Path
- 3.14 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
- NPAPI插件开发学习:Webkit的插件机制
- RabbitMQ (二)工作队列
- 如何:测试 UserControl 的运行时行为
- 自定义对象的归档 //类只有实现 NSCoding 协议才具备归档功能 归档最好定义成宏,笔误好发现.
- JAVA中的四种引用
- 3.14 Simplify Path
- NPAPI插件开发学习:NPAPI和NPRuntime的介绍
- 【剑指offer】面试题43:n个骰子的点数
- 如何使用Hadoop的Partitioner
- 利用URL Protocol实现网页调用本地应用程序
- Distinct Subsequences
- qsort()
- J2EE全面介绍
- LeetCode OJ Sort List