标签:des style color io java ar strong for div
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
"/../"?"/".‘/‘ together, such as
"/home//foo/"."/home/foo".public class Solution {
public String simplifyPath(String path) {
if(path==null||path.length()==0)
{
return path;
}
LinkedList<String> list=new LinkedList<String>();
int first,second;
String p;
for(first=1,second=0;first<path.length();first=second+1)
{
second=path.indexOf('/',first);
if(second==first){
continue;
}
if(second<0)
{
p=path.substring(first);
second=path.length();
}
else
{
p=path.substring(first,second);
}
if(p.equals("."))
{
continue;
}
if(p.equals(".."))
{
if(list.size()>0)
{
list.remove(list.size()-1);
}
continue;
}
list.add(p);
}
String dest="";
for(String element:list){
dest+="/"+element;
}
if(dest==""){
dest="/";
}
return dest;
}
}标签:des style color io java ar strong for div
原文地址:http://blog.csdn.net/jiewuyou/article/details/39393735