最长的文件路径 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\ ...
随机推荐
- MFC CFile类读写文件详解
CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 一个读写文件的例子: 文件I/O 虽然使用CArchive类内建的序列化 ...
- Kafka集群监控工具之二--Kafka Eagle
基于kafka: kafka_2.11-0.11.0.0.tgz kafka-eagle-bin-1.2.1.tar.gz 1.下载解压 tar -zxvf kafka-eagle-bin-1.2.1 ...
- 消息系统之Apache ActiveMQ
一.下载运行MQ服务 1.下载ActiveMQ :http://activemq.apache.org/ 2.解压缩: 进入bin目录 win32和win64对应不同位的操作系统,选择进入 点击act ...
- 如何合并两个Git仓库
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- MySQL Crash Course #17# Chapter 25. 触发器(Trigger)
推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...
- Python3.x(windows系统)安装requests库
Python3.x(windows系统)安装requests库 cmd命令: pip install requests 执行结果:
- Django 搭建后台 favicon.ico 文件操作
Django 搭建后台 favicon.ico 文件操作 使用 django 搭建后台服务器,我用的是 django 1.8 版本以上的,就是路由不是 url 而是 path 的,这里有一个关于fav ...
- codevs1001 舒适的路线 - 贪心 - 并查集
题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤ ...
- 在awk中通过system调用sql语句来说明引号的使用
一个实际应用例子: 在awk中,通过system调用连接数据库并作select操作,select语句中where条件来自于一个文件(file)的第一个域($1). $ cat file ... ... ...
- VC++ 实现修改文件创建、访问、修改时间属性(转载)
转载:http://sunnysab.blog.163.com/blog/static/18037500920134221295425/ struct _FILETIME { //结构体定义 DWOR ...