[Leetcode] Simplify Path
来源:互联网 发布:法律法规数据库系统 编辑:程序博客网 时间:2024/06/10 13:04
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) { Stack<String> result = new Stack<>(); StringBuilder simplifiedPath = new StringBuilder(); int lastSlash = 0; String element = ""; for(int i = 1; i < path.length(); i++) { if(path.charAt(i) == '/') { element = path.substring(lastSlash + 1, i); lastSlash = i; } else if(i == path.length() - 1) { element = path.substring(lastSlash + 1, path.length()); } else { continue; } if(element.equals(".") || element.equals("")){ continue; } else if(element.equals("..") ){ if(!result.empty()){ result.pop(); } } else if(element.equals("/")){ result.clear(); } else { result.push(element); } } if(result.empty()){ simplifiedPath.append("/"); } else{ for(String pathEle : result) { simplifiedPath.append("/").append(pathEle); } } return simplifiedPath.toString(); }}
0 0
- LeetCode: Simplify Path
- LeetCode Simplify Path
- LeetCode : Simplify Path
- [Leetcode] Simplify Path
- [leetcode] simplify path
- [LeetCode] Simplify Path
- leetcode 100: Simplify Path
- [leetcode]Simplify Path
- [LeetCode]Simplify Path
- [leetCode] Simplify Path
- [leetcode]Simplify Path
- [leetcode] Simplify Path
- [LeetCode] Simplify Path
- leetcode simplify path
- Leetcode: Simplify Path
- LeetCode题解:Simplify Path
- [LeetCode] Simplify Path
- LeetCode Simplify Path
- javascript函数声明与函数表达式
- Java常用8大排序算法
- 数据库 Oracle设置口令到达终止时间后的宽限天数
- 最小生成树(普利姆算法、克鲁斯卡尔算法)(逻辑理解小结)
- OpenCV代码提取:flip函数的实现
- [Leetcode] Simplify Path
- 关闭浏览器后Session失效原因分析
- KMP算法小结
- svn原理----revert,回滚
- 短网址(短链)实现唤起App
- MSSQL中存储过程的可选参数的定义和使用_SQL高亮显示
- 次小生成树
- Java的四种引用,强弱软虚,用到的场景。
- 解决Xcode 8 Console 输出系统网络连接log的问题