71. Simplify Path
来源:互联网 发布:icloud优化存储空间 编辑:程序博客网 时间:2024/06/07 18:03
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"
.
1.注意使用正则表达式进行分隔
2.注意分隔后的数组 存在“”的情况 需要处理
3.熟悉switch的用法
switch(x){
case"xxx": break;/continue;
}
4.ArrayList<>() 数组列表
public class Solution { public String simplifyPath(String path) { String []s=path.split("\\/+"); ArrayList<String> st=new ArrayList<>(); for(String c:s){ switch(c){ case"": case".": continue; case"..": if(st.size()>0) st.remove(st.size()-1); break; default: st.add(c); break; } } if(st.size()==0) return "/"; StringBuilder sb = new StringBuilder(); for(String sd : st){ sb.append("/"); sb.append(sd); } return sb.toString(); }}
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
- 滴滴笔试-地下迷宫
- Python filter 和map
- 使用Fragment实现tabhost效果
- Linux下的 FTP
- 【那些年遇到过的面试题】考虑如何将一个vector 赋给另一个vector
- 71. Simplify Path
- 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
- c_str
- 自己实现FlowLayout来教你自定义ViewGroup
- Hibernate flush详解
- Error setting expression 'XXX.XXX' with value '[Ljava.lang.String;@23b0f28f'ssh插入外键字段报错
- hybrid app开发工具
- 关于秒杀业务的相关处理
- win7怎样取消Mysql自动启动?