Leetcode 71. Simplify Path
2016-01-29 23:36
423 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
2 if the string splited is '..', then if the
stack is not empty, then just pop the stack.
3 if the string is not the case above, then
just push the string into the stack.
4 and then pop the string in the stack in
the order and construct the final result.
the only way I know is to append the poped
string to the end of the result string.w
public class Solution {
public String simplifyPath(String path) {
if(path == null || path.length() == 0)
return path;
Stack<String>stack = new Stack<String>();
String[] temRes = path.split("/");
for(int i = 0; i < temRes.length; i++){
if(temRes[i].equals(".") || temRes[i].length() == 0)
continue;
else if(temRes[i].equals("..")){
if(!stack.isEmpty()){
stack.pop();
}
}
else{
stack.push(temRes[i]);
}
}
//I use two stack to reverse the make the order of the string the same with the original string
Stack<String> reverse = new Stack<String>();
StringBuffer result = new StringBuffer();
while(!stack.isEmpty()){
reverse.push(stack.pop());
}
while(!reverse.isEmpty()){
result.append("/" + reverse.pop());
}
if(result.length() == 0)
result.append("/");
return result.toString();
}
}
For example,
path =
"/home/", =>
"/home"
path =
"/a/./b/../../c/", =>
"/c"
So why we need to change the format of the String address. cause the format of the string address given is not a standard
format, so we need to turn it into a standard format.
The address string is split by some string, then we need to use this string array to construct the result..
There are different condition
1 if the string array is '.' or '' that means that the the length of the string is 0, then just
continue,
2 if the string splited is '..', then if the
stack is not empty, then just pop the stack.
3 if the string is not the case above, then
just push the string into the stack.
4 and then pop the string in the stack in
the order and construct the final result.
the only way I know is to append the poped
string to the end of the result string.w
public class Solution {
public String simplifyPath(String path) {
if(path == null || path.length() == 0)
return path;
Stack<String>stack = new Stack<String>();
String[] temRes = path.split("/");
for(int i = 0; i < temRes.length; i++){
if(temRes[i].equals(".") || temRes[i].length() == 0)
continue;
else if(temRes[i].equals("..")){
if(!stack.isEmpty()){
stack.pop();
}
}
else{
stack.push(temRes[i]);
}
}
//I use two stack to reverse the make the order of the string the same with the original string
Stack<String> reverse = new Stack<String>();
StringBuffer result = new StringBuffer();
while(!stack.isEmpty()){
reverse.push(stack.pop());
}
while(!reverse.isEmpty()){
result.append("/" + reverse.pop());
}
if(result.length() == 0)
result.append("/");
return result.toString();
}
}
相关文章推荐
- 抱歉最近朋友结婚去浪了几天~未来几天会正常更新.今天带来XML文件解析
- 随笔3 视频课程—寒假计划
- 双色球、大乐透、3D精选版!祝君好运(Javascript)
- SQL 笔记
- Andoird适配问题-ViewPager页面滑动卡顿
- 回归
- 最长相同子串问题(一)
- 高级PHP应用程序漏洞审核技术
- 白书练习 6174问题
- php 整型 和 字符串相加
- 4种sql分页
- 视音频数据处理入门:PCM音频采样数据处理
- 闭包初探
- glibc内存泄露以及TCmalloc 简单分析
- 第三百零二天 how can I 坚持
- 关于web前端一些常见bug及解决方案
- Java生成不重复的数的方法(精确到毫秒级别)
- Android之操作相册
- js图片跑马灯效果
- Btspread Search Assistant的开发过程