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

\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. get值乱码(gbk编码浏览器造成)

     $condition = urldecode($condition); 即可

  2. Objective-C中的const ,extern,static

    一.const 1>对于const,记住关键的一点,它只是修饰右边的变量. 例如: - (void)viewDidLoad { [super viewDidLoad]; // const两种用法 ...

  3. C++顺序容器类总结

    主要是vector,deque,list,forward_list,array,string 插入方法: 元素访问: 元素删除: 容器赋值: forward_list有很多特殊的方法 毕竟平时forw ...

  4. Emmet Documentation

    src:http://docs.emmet.io/cheat-sheet/ Emmet Documentation Syntax   Child: > nav>ul>li <n ...

  5. 转 Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...

  6. 【原】jQuery编写插件

    分享一下编写设置和获取颜色的插件,首先我将插件的名字命名为jquery.color.js.该插件用来实现以下两个功能1.设置元素的颜色.2.获取元素的颜色. 先在搭建好如下编写插件的框架: ;(fun ...

  7. AD RMS Bulk Protection Tool 批量加密解密office文档

    1.Active Directory Rights Management Services Bulk Protection Tool  http://www.microsoft.com/zh-cn/d ...

  8. Entity Framework关联实体的三种加载方法

    推荐文章 EF性能之关联加载 总结很好 一:介绍三种加载方式 Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和熊掌 ...

  9. Json 返回日期格式转换

    //日期转换 function ChangeDateFormat(time) { if (time != null) { var date = new Date(parseInt(time.repla ...

  10. 【网络流24题】 No.10 餐巾计划问题 (线性规划网络优化 最小费用最大流)

    [题意] 一个餐厅在相继的 N 天里, 每天需用的餐巾数不尽相同. 假设第 i 天需要 ri 块餐巾(i=1,2,-, N). 餐厅可以购买新的餐巾,每块餐巾的费用为 p 分:或者把旧餐巾送到快洗部, ...