Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Subscribe to see which companies asked this question
.不管 ..出栈 其他入栈
public class Solution {//http://blog.csdn.net/makuiyu/article/details/44497901
public String simplifyPath(String path) {
Stack <String> list = new Stack<>();
for(int i =0;i<path.length();){
while(i<path.length()&&path.charAt(i)=='/'){
i++;
}
String s = "";
while(i<path.length()&&path.charAt(i)!='/'){
s+=path.charAt(i);
i++;
}
if(s.equals("..")){
if(!list.empty())list.pop();
}else{
if(!(s.equals("")||s.equals(".")||s.equals(".."))){
list.push(s);
}
}
}
String ret = "";
if(list.empty()){
return "/";
}
while(!list.empty()){
ret="/"+list.pop()+ret;
}
return ret;
}
}