• 388. Longest Absolute File Path


    就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关。

    表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测.
    之后的 表示层数。
    思路是如果当前层数多余已经有的层数说明是在go deeper,就继续,否则就要回到他属于的父目录,这个性质用STACK来实现再好不过。。

    然后一开始没注意求的是最大长度,所以想的是往里PUSH文件夹的名字,其实因为只需要长度,光PUSH文件夹名字的长度就行了。。

    用split(" ")来分,注意 是他妈一个字符,一开始蠢了,以为是2个。。

    public class Solution {
        public int lengthLongestPath(String input) 
        {
            int res = 0;
            if(input.length() == 0) return res;
            
            Stack<Integer> stk = new Stack<Integer>();
            stk.push(0);
            String[] s = input.split("
    ");
            
            for(int i = 0; i < s.length;i++)
            {
                
                int level = s[i].lastIndexOf("	") + 1; //+1 because it starts with 0. -1 means none
                int tempLength =  0;
               
                    //dir  
       	subdir1  
        	subdir2  
        		file.ext
                    
                    while(stk.size()-1 > level) stk.pop();
                                 //father dir length + cur length  - number of 	 + "/"
                    tempLength = stk.peek() + s[i].length() - level + 1;
                    
                    
                    //u can push file into it, it will be poped anyway.
                    stk.push(tempLength);
                
    
                // we only look for files' path
                if(s[i].contains(".")) res = Math.max(res,tempLength);
                
                
            }
                // no file will return 0
            if(res == 0) return 0;
                    // first dir does not have "/"
            return res-1;
        }
    }
    

    二刷。

    特别像DFS,用Stack来记录回溯的位置。

    开始用Split按照 分开,然后开始数 的数量,代表所在的层数,深于当前层说明继续深入搜索;否则回溯。

    当前层的深度就是stk的size,一开始存的string,快做完发现其实不需要,直接存字符的长度就可以。
    深度搜索前要把当前的字符长度加入到stk里,如果是dir得 + 1作为代表/的长度;不是的话更新下最长长度,再直接加进去,反正往后要pop出来。(也可以不加,因为下一次肯定要POP出来。。)

    Time : O(n)
    Space : O(n)

    一刷应该是看了答案,二刷自己写的= =所以不如一刷简洁。

    public class Solution {
        public int lengthLongestPath(String input) {
            if (input.length() == 0) return 0;
            
            Stack<Integer> stk = new Stack<>();
            
            String[] paths = input.split("
    ");
            
            
            int tempMax = 0;
            int tempStkMax = 0;
            
            for (int i = 0; i < paths.length; i++) {
                String s = paths[i];
                //System.out.println("Current s is " + s);
                int j = 0;
                int temp = 1;
                while (j < s.length() && s.charAt(j) == '	') {
                    temp ++;
                    j ++;
                }
                //System.out.println("level is: " + temp + " stk.size() is " + stk.size());
                String pathName = s.substring(j);
                //System.out.println("pathName : " + pathName);
                while (temp <= stk.size()) {
                    tempStkMax -= stk.pop();
                }
                
                
                //System.out.println("Length of all strings in stk: " + tempStkMax);
                int ext = pathName.indexOf(".");
                if (ext != -1) {
                    //stk.add(pathName.length());
                    //tempStkMax += pathName.length();
                    tempMax = Math.max(tempMax, tempStkMax + pathName.length());
                } else {
                    stk.add(pathName.length()+1);
                    tempStkMax += pathName.length()+1;
                }
            
                
            }
    
            return tempMax;
            
            
        }
    }
    
  • 相关阅读:
    从零开始学Kotlin-使用接口(7)
    从零开始学Kotlin-类的继承(6)
    从零开始学Kotlin-类和对象(5)
    从零开始学Kotlin-控制语句(4)
    从零开始学Kotlin-操作符(3)
    从零开始学Kotlin-数据类型(2)
    从零开始学Kotlin-基础语法(1)
    Java设计模式之单例模式(七种写法)
    一个简单的可控的头像列表叠加控件
    使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突
  • 原文地址:https://www.cnblogs.com/reboot329/p/5875867.html
Copyright © 2020-2023  润新知