Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
算法:根據/把path分割,再逐個判斷
public class Solution { public String simplifyPath(String path) { String[] strs = path.split("/"); Stacks = new Stack (); for(int i=0;i 0){ s.push(strs[i]); } } StringBuilder sb = new StringBuilder(); Iterator it = s.iterator(); while(it.hasNext()){ String t = it.next(); sb.append("/").append(t); } String r=sb.toString(); if(r.length()==0) r+="/"; return r; } }