LeetCode笔记——71简化路径

题目:

给定一个文档 (Unix-style) 的完全路径,请进行路径简化。

例如,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

边界情况:

  • 你是否考虑了 路径 = "/../" 的情况?
    在这种情况下,你需返回 "/" 。
  • 此外,路径中也可能包含多个斜杠 '/' ,如 "/home//foo/" 。
    在这种情况下,你可忽略多余的斜杠,返回 "/home/foo" 。

思路:这个题的题目有点不太懂。在网上参考了大神的代码,先把代码弄清楚。原文地址:https://blog.csdn.net/fightforyourdream/article/details/16917563

基本的思路是首先把字符串用/分割为字符数组;然后依次对字符进行判断将符合条件的放入栈或者队列中;最后转化为字符输出。在以下两种代码中,使用了集合linkedlist.该集合中包含了栈和队列的方法,也就是说可以当成栈和队列使用。其中栈的操作为pop(),push(),操作在链表头部进行;队列的操作为add(),remove(),进队列从尾部进,在头部出队列。以下两种代码分别是栈和队列的实现。

代码:

public class Solution {
    public String simplifyPath(String path) {
        String[] ss = path.split("/");  //使用/将字符串分解为字符数组。新的字符数组中不在包括/
        LinkedList<String> ll = new LinkedList<String>();
        for(int i=0; i<ss.length; i++) {
            if(!ll.isEmpty() && ss[i].equals("..")) {
                ll.removeLast();   //删除".."前添加的字符
            } else if(ss[i].length() != 0 && !ss[i].equals(".") && !ss[i].equals("..")){
                ll.add(ss[i]);    //将满足条件的字符加入到队列中
            }
        }
        
        if(ll.isEmpty()) {
            return "/";
        }
        
        //String s = "";
        StringBuilder s=new StringBuilder("");  //使用StringBuilder来完成循环加入字符
        while( !ll.isEmpty() ) {
           // s += "/";
            s.append('/');  //append()添加字符
            //s += ll.remove();
            s.append(ll.remove());
        }
        return s.toString();   //将结果转化为字符返回
    }
}


public static String simplifyPath(String path) {
        if(path.length() == 0){
            return path;
        }
        
        String[] splits = path.split("/");
        LinkedList<String> stack = new LinkedList<String>();
        for (String s : splits) {
            if(s.length()==0 || s.equals(".")){
                continue;
            }else if(s.equals("..")){
                if(!stack.isEmpty()){
                    stack.pop();
                }
            }else{
                stack.push(s);
            }
        }
        
        if(stack.isEmpty()){
            stack.push("");
        }
        String ret = "";
        while(!stack.isEmpty()){
            ret += "/" + stack.removeLast();
        }
        
        return ret;
    }


执行最快的代码:

这段代码的思路是使用两个指针依次取出字符串中两个//之间的字符;然后进行判断,满足条件的添加到ArrayList中;然后再给结果添加/输出,这一步也是使用的StringBuilder.

class Solution {
    public String simplifyPath(String path) {
        List<String> dirs = new ArrayList<>();
        
        int dirStart = 0, len = path.length();
        while (dirStart < len) {
            // consume one slash and the string
            // find the index of directory
            while (dirStart < len && path.charAt(dirStart) == '/') dirStart++;
            
            int dirEnd = dirStart;
            while (dirEnd < len && path.charAt(dirEnd) != '/') dirEnd++;
            
            String dir = path.substring(dirStart, dirEnd);
            if (dir.equals(".")) {
            }
            else if (dir.equals("..")) {
                if (! dirs.isEmpty()){
                    dirs.remove(dirs.size() - 1);
                }
            } else {
                if (dir.length() > 0) {
                    dirs.add(dir);
                }
            }
            dirEnd++;
            dirStart = dirEnd;
        }
        
        // combine all dirs
        StringBuilder sb = new StringBuilder("/");
        for (int i = 0; i < dirs.size(); i++) {
            if (i == dirs.size() - 1) {
                sb.append(dirs.get(i));
            } else {
                sb.append(dirs.get(i)).append("/");
            }
        }
        
        return sb.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/chenxy132/article/details/83141629