leetcode 71. Simplify Path
来源:互联网 发布:log4j2 json 配置 编辑:程序博客网 时间:2024/05/22 03:22
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 String simplifyPath(String path) { Stack<String> result = new Stack<String>(); for(int i = 0;i < path.length();){ int j = i+1; while(j < path.length() && path.charAt(j) != '/') j++; //System.out.println(path.substring(i,j)); if(path.substring(i,j).equals("/")) ; else if(path.substring(i,j).equals("/.")) ; else if(path.substring(i,j).equals("/..")){ if(!result.empty()) result.pop(); } else{ result.push(path.substring(i,j)); } i = j; } if(result.empty()) return "/"; StringBuffer temp = new StringBuffer(); for(String str:result) temp.append(str); return temp.toString(); }
教师里讨论问题真的很。。。。
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- [leetcode] 71. Simplify Path
- LeetCode 71. Simplify Path
- LeetCode - 71. Simplify Path
- [LeetCode] 71. Simplify Path
- leetcode 71. Simplify Path
- Java初级认证 学习体会 20160922
- 5.5
- 使用SwipeRefreshLayout和自定义的PullToReFreshListView实现下拉刷新和上拉加载更多
- 清除行列
- 进程间通信之kill发送信号
- leetcode 71. Simplify Path
- 正确使用AFNetworking的SSL保证网络安全
- js复制文字信息
- 设计模式之三:工厂方法模式—多态工厂的实现
- Fresceo使用详解--用Java代码实现xml配置的效果(二)
- 对于分布式系统的理解
- Objective-C-iOS AppDelegate代理详解(启动,打开App,推送,通知)
- 常用开发工具----搜索工具
- 67. magento timezone