Simplify Path

2015-01-27 06:10:28 · 作者: · 浏览: 7

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("/");
        Stack
  
    s = 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; } }