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

\n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测.

之后的\t表示层数。

思路是如果当前层数多余已经有的层数说明是在go deeper,就继续,否则就要回到他属于的父目录,这个性质用STACK来实现再好不过。。

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

用split("\n")来分,注意\t \n是他妈一个字符,一开始蠢了,以为是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("\n"); for(int i = 0; i < s.length;i++)
{ int level = s[i].lastIndexOf("\t") + 1; //+1 because it starts with 0. -1 means none
int tempLength = 0; //dir \n \tsubdir1 \n \tsubdir2 \n \t\tfile.ext while(stk.size()-1 > level) stk.pop();
//father dir length + cur length - number of \t + "/"
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按照\n分开,然后开始数\t的数量,代表所在的层数,深于当前层说明继续深入搜索;否则回溯。

当前层的深度就是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("\n"); 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) == '\t') {
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; }
}

388. Longest Absolute File Path的更多相关文章

  1. [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  2. 【leetcode】388. Longest Absolute File Path

    题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...

  3. 【LeetCode】388. Longest Absolute File Path 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...

  4. 388 Longest Absolute File Path 最长的绝对文件路径

    详见:https://leetcode.com/problems/longest-absolute-file-path/description/ C++: class Solution { publi ...

  5. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  6. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  7. Leetcode: Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  8. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. Leetcode算法比赛----Longest Absolute File Path

    问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...

随机推荐

  1. datatables完整的增删改查

    1.需要指定datatables的ID <button class="btn btn-primary" id="newAttribute">新增证照 ...

  2. 技术贴:asp.net实现唯一账户在线 禁止同一帐号同时在线 asp.net实现您的帐号在别处登录,您已被迫下线!

    技术要点: Application 全局变量的使用 hashtable 的使用 Session 对应唯一sessionID 标志会话状态 webpage 继承 BasePage的技术 整体比较简单,主 ...

  3. properties文件的继承(套用)关系

    现项目中有多个配置文件分布于/props____def.properties____/env_______def.propertiess_______/dev_______def.properties ...

  4. C语言-03流程控制

    1.选择结构 char c = '+'; ; ; // 如果要在case后面定义新的变量,必须用大括号{}包住 注意变量的作用域的紊乱 if语句不加括号时,也要注意此问题 switch (c) { c ...

  5. zzuli oj 1178 单词数

    Description 统计一篇文章里不同单词的总数. Input 有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output 每 ...

  6. 如何让ubuntu启动时打印字符信息----字符启动

    一.概述 要想实现字符启动,需要修改grub.cfg(启动配置文件),将“静态启动”改为“字符启动”. 但是grub.cfg通常只作为只读文件,修改它时实际上修改的是其他的文件然后再通过update- ...

  7. Servlet+Tomcat制作出第一个运行在Tomcat上的Java应用程序

    转载自:http://www.linuxidc.com/Linux/2011-08/41685.htm [日期:2011-08-27] 来源:csdn  作者:Cloudyxuq     1.IDE工 ...

  8. 【技术贴】Eclipse 右键打开当前文件所在文件夹

    1.使用插件,百度:OpenExplorer_1.5.0.v201108051513.jar 2.默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explore ...

  9. 禁止form表单回车键进行提交

    使用EasyUI的时候,我们会给一个datagrid添加一个搜索栏,但是这个搜索栏的form,我们一般使用ajax向服务器提交数据,因此在这样一个环境下可以考虑禁止用户按回车键(Enter)提交.代码 ...

  10. [转贴]C++、C#写的WebService相互调用

    以下宏文(原文在 http://blog.sina.com.cn/s/blog_4e7d38260100ade4.html),是转贴并进行了修饰编辑: 首先感谢永和兄提供C++的WebService服 ...