最长的文件路径 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\ ...
随机推荐
- kendo datetimepicker
@(Html.Kendo().DatePicker() .Name("yearCalendar") .Value(DateTime.Now) .Start(CalendarView ...
- Object-C-NSDictionary
存储对象都必须是id(对象类型)不能使基础类型 NSDictionary *scores=[[NSDictionary alloc]initWithObjectsAndKeys:@"89&q ...
- EditPlus 中文版停止更新
经过一段时间的考虑,我决定了终止 EditPlus 的中文版翻译工作. 感谢各位网友在这么长时间内对本汉化项目的关注.
- Azkaban-开源任务调度程序(使用篇)
上篇文章说到了安装,这次说说使用 登录 https://localhost:8443 注意是https,采用的是jetty ssl链接.输入账号密码azkaban/azkanban(如果你之前没有更改 ...
- HTTP从入门到入土(5)——HTTP报文格式
HTTP报文格式 HTTP报文分为请求报文和响应报文,只有发送了请求报文,才会有响应报文. 常见的报文格式如下所示:
- echart知识点、常用图形
原文地址:https://www.cnblogs.com/kewenxin/p/9338272.html 本文是自己在项目中需要运用到的echarts图形进行整理,都有完整的代码.echarts原型, ...
- python-随机数的产生random模块
random模块用来产生随机数: 查看random模块的方法: import random random.__dir__ Out[39]: <function __dir__> rando ...
- Java 执行jar文件出现版本错误信息
Java 执行jar文件出现版本错误信息 一.问题 执行jar文件出现如下错误信息: 二.解决方案 是因为在创建工程的时候选择的jdk编译版本,和执行jar环境的jdk版本不一致: 更改工程的jdk版 ...
- c++的class声明及相比java的更合理之处
或许是基于一直以来c/c++头文件声明和cXX实现物理上置于独立文件的考虑,c++中的OO在现实中基本上也是按照声明和实现分离的方式进行管理和编译,如下所示: Base.h #pragma once ...
- 20145331魏澍琛 《网络对抗技术》 PC平台逆向破解
20145331魏澍琛 <网络对抗技术> PC平台逆向破解 学习任务 1.shellcode注入:shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中 ...