码迷,mamicode.com
首页 > 其他好文 > 详细

#leetcode#Simplify Path

时间:2015-07-25 07:11:54      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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

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

click to show corner cases.

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes ‘/‘ together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".
分析:用一个stack来模拟进入新文件夹或者返回上一层的操作,这里需要注意的是stack.toArray()的用法, 如果已知stack的parameter type,比如String或者Integer, 则可以直接得到一个相应type的array:   Integer[ ] arr = stack.toArray(new Integer[0]);

Java doc: 

Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:

     String[] y = x.toArray(new String[0]);

Note that toArray(new Object[0]) is identical in function to toArray().


public static void main(String[] args){
		LinkedList<Integer> stack = new LinkedList<>();
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		Integer[] arr = stack.toArray(new Integer[0]);
		for(int i = 0; i < arr.length; i++){
			System.out.println(arr[i]);
		}
		
	}

至于返回的顺序: 上面代码中的 arr 为 {4, 3, 2, 1},也就是按照 stack 依次 pop()得到的顺序。

回到Simplify Path这个题, 有一个小技巧,自己写的时候没有引入index这个变量,而是判断了一下 当前 StringBuilder 长度是否为0, 觉得有点不妥但是没有想到好的办法。 学习了Code Ganker大神的写法,每次循环开始的时候把 i 的值赋给 index,如果 当前元素是 ‘/’, 则 i 的值没变, 就跳过了下面的判断。

public class Solution {
    public String simplifyPath(String path) {
        if(path == null || path.length() == 0){
            return "";
        }
        StringBuilder res = new StringBuilder();
        LinkedList<String> stack = new LinkedList<>();
        int i = 0;
        while(i < path.length()){
            StringBuilder item = new StringBuilder();
            int index = i;
            while(i < path.length() && path.charAt(i) != '/'){
                item.append(path.charAt(i));
                i++;
            }
            if(index != i){ // 这里避免了在stringbuilder为空的情况下的多余判断
                if(item.toString().equals("..")){
                    if(!stack.isEmpty()){
                        stack.pop();
                    }
                }else if(item.toString().equals(".") /*|| item.length() == 0*/){
                    // do nothing
                }else{
                    stack.push(item.toString());
                }
            }
            i++;
        }
        
        if(stack.isEmpty()){
            return "/";
        }
        String[] arr = stack.toArray(new String[0]);
        for(int ii = arr.length - 1; ii >= 0; ii--){
            res.append('/');
            res.append(arr[ii]);
        }
        
        return res.toString();
    }
}







版权声明:本文为博主原创文章,未经博主允许不得转载。

#leetcode#Simplify Path

标签:leetcode

原文地址:http://blog.csdn.net/chibaoneliuliuni/article/details/47051479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!