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

\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. php 文件上传的基本方法

    基本思路:1.HTML表单中 form中的enctype必为enctype="multipart/form-data",method = post 设置提交数据中的type = f ...

  2. SQL 添加字段和默认值脚本

    --插入字段和默认值alter table Acc_WayBill add DeclaredValue nvarchar(50)goEXEC sys.sp_addextendedproperty @n ...

  3. HTML5的全局属性

    contentEditable:是否允许用户编辑元素中的内容.contentEditable有两个值,一个True 一个False. 例子: <ul contentEditable=" ...

  4. ASP.net+MVC--2

    1.ASP.NET MVC控制器 1)在Controllers文件夹下新建控制类 public class HelloWorld2Controller : Controller { public st ...

  5. 小笔记(二):php数组

    一.对于一二维数组重新组合为另一个二维数组,根据键值名称对一个二维数组进行重新组合例: /*$paramArray=array( * 'a'=>array('0'=>'1','1'=> ...

  6. 网易邮箱前端Javascript编码规范:基础规范

    在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...

  7. [转]PHP Session原理分析及使用

    之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...

  8. Vcl.FileCtrl.SelectDirectory

    描述:显示一个目录的对话框(属于Delphi) function SelectDirectory(var Directory: string; Options: TSelectDirOpts; Hel ...

  9. SDWebImage 清除缓存

    1.找到SDImageCache类 2.添加如下方法: - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *file ...

  10. uva 812 Trade on Verweggistan

    题意: 给w个货架, 每个货架上有bi个货物, 每次只能拿最上面的货物, 每个货物有个价值, 所有货物的售价均为10. 问:能获得的最大利润, 以及能获得这个利润需要多少个货物. (有多种组合时只需输 ...