LeetCode-Simplify Path
来源:互联网 发布:企业即时通讯软件 免费 编辑:程序博客网 时间:2024/05/16 01:04
这个题只要搞清楚什么是unix的path格式就好 就是后面没有/ 除了字母等有意义的字符以外 只有..有用 就是向上的意思
所以用stack 不是..也不是无用的就push 遇到..就pop
最后把stack里面的连起来 注意corner case 就是stack为空 但是遇到了。。 无法pop
但是也要忽略。。
public class Solution { public String simplifyPath(String path) { if ( path == null || path.length() == 0 ) return ""; Stack <String> stack = new Stack <String> (); for ( String str : path.split("/") ){ if ( str.equals("") || str.equals(".") || ( stack.isEmpty() && str.equals("..") )) continue; else if ( !stack.isEmpty() && str.equals("..") ) stack.pop(); else stack.push( str ); } if ( stack.isEmpty () ) return "/"; String res = ""; while ( !stack.isEmpty() ){ res = "/" + stack.pop() + res; } return res; }}
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
- LeetCode_Array_Easy
- 外部排序
- 一起来做chrome扩展《使用代理proxy》
- Service API Guides
- 黑马程序员_javaCode的数组2
- LeetCode-Simplify Path
- SnapChat 面试题:HTML Parser
- my frist linux
- Wildcard matching
- Log4j2使用案例
- POJ 2676 解题报告
- [Leetcode]Letter Combinations of a Phone Number My Submissions Question Solution
- [汇编语言]debug中用r命令修改cs、ip,并不能影响a命令的代码起始写入地址?
- HtmlUnit模拟登录网站时注意事项