最长的文件路径 Longest Absolute File Path
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的更多相关文章
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode算法比赛----Longest Absolute File Path
问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode: Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
随机推荐
- GIC400简介
GIC400是arm公司的中断控制IP,提供axi4接口,主要功能: 1)中断的使能(enable,mask); 中断的优先级(poriority); 中断的触发条件(level-sensitive ...
- SV中的task和function
SV中class的properties和methods默认都是public的,但是可以声明为local和protected. 一个properties声明为local类型的,则只在该class中的me ...
- Hive 中Join的专题---Join详解
1.什么是等值连接? 2.hive转换多表join时,如果每个表在join字句中,使用的都是同一个列,该如何处理? 3.LEFT,RIGHT,FULL OUTER连接的作用是什么? 4.LEFT或RI ...
- php mysqli query 查询数据库后读取内容的方法
php mysqli query 查询数据库后读取内容的方法 <?php$mysqli = new mysqli("localhost", "my_user&quo ...
- Python Web学习笔记之进程与线程
要了解二者的区别与联系,首先得对进程与线程有一个宏观上的了解. 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竟争计算机系统资源的基本单位.每一个进程都有一个自己的地址空 ...
- Linux 命令安装bin文件
Linux 命令安装bin文件 安装命令: //1,增加文件的可执行权限 chmod a+x jdk-6u30-linux-x64.bin //2,程序即安装在执行命令的文件夹下 ./jdk-6u30 ...
- JavaScript 表单验证 案例
JavaScript 表单验证 案例 版权声明:未经授权,严禁转载! 编写 HTML 文件,搭建主体界面 <html> <head> <meta charset=&q ...
- 20145212罗天晨 逆向及Bof基础实践
20145212罗天晨<网络对抗>第1周学习总结--逆向及Bof基础实践 逆向及Bof基础实践 一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代 ...
- C#调用托管ocx、dll
前篇文章是调用非托管,比较复杂,这里是调用托管,很简单[所以在遇到非托管dll时可以通过二次封装成托管的方式,再通过这边文章来使用] 1.注意这是基于COM的ocx或者dll,所以用regsvr32先 ...
- Python3基础 list remove 删除元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...