71. Simplify Path

2016-12-14  本文已影响0人  夜皇雪
public class Solution {
    public String simplifyPath(String path) {
        String res="/";
        String[] sub=path.split("/+");
        ArrayList<String> paths=new ArrayList<>();
        for(String s: sub){
            if(s.equals("..")){
                if(paths.size() > 0){
                    paths.remove(paths.size() - 1);
                }
            }else if(!s.equals(".")&&!s.equals("")){
                paths.add(s);
            }
        }
        for(String s:paths) res+= s+"/";
        if(res.length()>1) res=res.substring(0,res.length()-1);
        return res;
    }
}
上一篇下一篇

猜你喜欢

热点阅读