首页 > 代码库 > leetcode Simplify Path

leetcode Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

1,java里string的函数split可以用来拆分string。拆分之后放入数组里

2,stringbuffer 的insert功能

3. 记得string 只能用equal

public class Solution {    public String simplifyPath(String path) {         Stack<String> store= new Stack<String>();        StringBuffer result=new StringBuffer();        String[] temp=path.split("/");        for(int i=0;i<temp.length;i++){        	if(temp[i].equals(".")){        		continue;        	}        	else if(temp[i].equals("..")){        		if(!store.isEmpty()){        			store.pop();        		}        	}        	else if(!temp[i].isEmpty()){        		store.push(temp[i]);        	}        }        while(!store.isEmpty()){        	result.insert(0,"/"+store.pop());                }        if(result.length()==0){             result.append("/");        }        return result.toString();    }}

  

leetcode Simplify Path