题意理解:
简化路径名;
题目分析:
1. 注意 “.” 、".."特殊字符的处理;
2. 运用栈;
解题代码:
public class Solution {
public String simplifyPath(String path) {
String ans="/";
if(path.length()==0){
return ans;
}
Stack<String> stack=new Stack<String>();
String[] splitVal=path.split("/");
for(int i=0; i<splitVal.length;i++){
if(splitVal[i].equals(".")){
continue;
}else if(splitVal[i].equals("..")){
if(!stack.empty()){
stack.pop();
}
}else if(splitVal[i].length()!=0){
stack.push(splitVal[i]);
}
}
while(!stack.empty()){
if(ans.equals("/")){
ans+=stack.pop();
}else{
ans="/"+stack.pop()+ans;
}
}
return ans;
}
}