最长的文件路径 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\ ...
随机推荐
- 浏览器内核控制标签meta说明
由于众所周知的原因,国内的主流浏览器都是双核浏览器:基于Webkit的内核用于常用网站的高速浏览,基于IE的内核主要用于部分网银.政府.办公系统等网站的正常使用.以360浏览器为例,我们优先通过Web ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON HomMat2dRotate1
zw版[转发·台湾nvp系列Delphi例程]HALCON HomMat2dRotate1 procedure TForm1.Button1Click(Sender: TObject);var img ...
- POJ 1836
刚开始二分写错了 wa了很久 这个二分 的好好想想 #include <iostream> #include<cstdio> #include<string.h> ...
- 论文笔记:语音情感识别(三)手工特征+CRNN
一:Emotion Recognition from Human Speech Using Temporal Information and Deep Learning(2018 InterSpeec ...
- Scrapy: 初识Scrapy
1.初识Scrapy Scrapy是为了爬取网站数据,提取结构性数据而编写的应用框架.可以应用在包括数据挖掘,信息处理或者存储历史数据等一系列的程序中. 2.选择一个网站 当需要从某个网站获取信息时, ...
- Java 简明教程
本文为 Java 的快速简明教程,主要用于快速了解.学习和复习java的语法特点. // 单行注释 /* 多行注释 */ /** JavaDoc(Java文档)注释是这样的.可以用来描述类和类的属性. ...
- 数据仓库基础(四)ODS、元数据
本文转载自:http://www.cnblogs.com/evencao/archive/2013/06/14/3135691.html ODS的概念:是一个面向主题的.集成的.可变的.反应当前细节的 ...
- Python-sys模块,异常
习题1:题目:给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. #encoding=utf-8 while True: try: num=int(raw_input(&quo ...
- 解决国内 NPM 安装依赖速度慢问题
不知道各位是否遇到这种情况,使用NPM(Node.js包管理工具)安装依赖时速度特别慢,为了安装Express,执行命令后两个多小时都没安装成功,最后只能取消安装,笔者20M带宽,应该不是我网络的原因 ...
- 特征提取的综合实验(多种角度比较SIFT、SURF、BRISK、ORB算法)
代码:https://files.cnblogs.com/files/jsxyhelu/main.zip 一.基本概念: 特征点提取在“目标识别.图像拼接.运动跟踪.图像检索.自动定位”等研究中起着重 ...