2018-07-30 22:05:52

问题描述:

问题求解:

本题个人感觉还是挺有意思的,题目要求的是最长的文件路径,其实是需要keep tracking路径长度,如果出现文件则需要进行比较,看是否为当前的最大长度。

难点就在于如何keep tracking,不妨将文件的路径旋转90度,那么就可以看到很明显的层次结构,我们可以使用一个栈来维护不同层次的信息,想到这里本题其实基本已经解决了一大半,剩下的就是层次关系的判断,显然和\t有关,那么对这个再进行分析,就很快可以得到解。

这里需要注意的是\t是算作一个字符的,并非两个字符。

    public int lengthLongestPath(String input) {
int res = 0;
String[] layers = input.split("\n");
Stack<Integer> stack = new Stack<>();
stack.push(0);
for (String layer : layers) {
int numOfTab = layer.lastIndexOf("\t") + 1;
int level = numOfTab + 1;
while (level < stack.size()) stack.pop();
int curLen = stack.peek() + layer.length() - numOfTab + 1;
stack.push(curLen);
if (layer.contains(".")) res = Math.max(res, curLen - 1);
}
return res;
}

最长的文件路径 Longest Absolute File Path的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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. Longest Absolute File Path -- LeetCode

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

  9. 【leetcode】388. Longest Absolute File Path

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

随机推荐

  1. zw版【转发·台湾nvp系列Delphi例程】HALCON HistoToThresh1

    zw版[转发·台湾nvp系列Delphi例程]HALCON HistoToThresh1 procedure TForm1.Button1Click(Sender: TObject);var imag ...

  2. uva11107 后缀数组

    题意给了n个串 然后计算 这些串中的子串在大于1/2的串中出现 求出这个串的最长长度. 将这些串用一个每出现的不同的字符拼起来 ,然后二分找lcp #include <iostream> ...

  3. linux服务器文件索引inodes满了

    inode节点中,记录了文件的类型.大小.权限.所有者.文件连接的数目.创建时间与更新时间等重要的信息,还有一个比较重要的内容就是指向数据块的指针.一般情况不需要特殊配置,如果存放文件很多,需要配置. ...

  4. web前端----jQuery事件

    事件 常用事件 click(function(){...}) hover(function(){...}) blur(function(){...}) focus(function(){...}) c ...

  5. MySQL数据库----触发器

    触发器-trigger 触发器:监视某种情况,并触发某种操作. 使用触发器可以定制用户对表进行[增.删.改]操作时前后的行为,注意:没有查询 -- 触发器:某种程序触发了工具的运行 -- 触发器不能主 ...

  6. pyDay4

    内容来自廖雪峰的官方网站 1.关键字参数有什么用?它可以扩展函数的功能. 2.参数定义的顺序必须是:必选参数.默认参数.可变参数.命名关键字参数和关键字参数. 3.对于任意函数,都可以通过类似func ...

  7. 来了解一下Ajax是什么?Ajax的原理?Ajax与传统Web比较?Ajax的优缺点?Ajax的Post与Get比较

    一.什么是Ajax Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简单地说,在不需要重新刷新页面的情况下 ...

  8. "1130-host ... is not allowed to connect to this MySql server"登录失败

    原因: 该用户没有远程连接权限. 解决:授权! mysql>GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' ...

  9. 20165310_Exp2实验二《Java面向对象程序设计》

    实验二<Java面向对象程序设计> TDD与单元测试 前期准备: 什么是单元测试? 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含 ...

  10. tensorflow mnist 给一张手写字辨别

    https://www.jianshu.com/p/db2afc0b0334 https://blog.csdn.net/xxzhangx/article/details/54563574