首页 > 其他 > 详细

leetcode Simplify Path

时间:2014-11-16 07:05:30      阅读:264      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/lilyfindjobs/p/4100946.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!