Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
represents:
dir
subdir1
subdir2
file.ext
The directory dir
contains an empty sub-directory subdir1
and a sub-directory subdir2
containing a file file.ext
.
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir
contains two sub-directories subdir1
and subdir2
. subdir1
contains a file file1.ext
and an empty second-level sub-directory subsubdir1
. subdir2
contains a second-level sub-directory subsubdir2
containing a file file2.ext
.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext"
, and its length is 32
(not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0
.
Note:
- The name of a file contains at least a
.
and an extension. - The name of a directory or sub-directory will not contain a
.
.
Time complexity required: O(n)
where n
is the size of the input string.
Notice that a/aa/aaa/file1.txt
is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png
.
分析:用stack的size来记录level的个数,stack里面的值是每一个level对应的长度。每次存入一个已经存在的level的时候,会把以前同一个level的清空。
public class Solution {
public int lengthLongestPath(String input) {
String[] tokens = input.split("\n");
int longestPath = , currLength = ;
Stack<Integer> stack = new Stack<>(); for (String s : tokens) {
int level = s.lastIndexOf("\t") + ; while (stack.size() > level) {
currLength -= stack.pop();
}
int length = s.replace("\t", "").length();
currLength += length;
stack.push(length); if (s.contains(".")) {
longestPath = Math.max(currLength + level, longestPath);
}
}
return longestPath;
}
}
Longest Absolute File Path的更多相关文章
- [LeetCode] 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 ...
- [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 ...
- 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\n\tsub ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- 388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- rpc框架之avro 学习 1 - hello world
avro是hadoop的一个子项目,提供的功能与thrift.Protocol Buffer类似,都支持二进制高效序列化,也自带RPC机制,但是avro使用起来更简单,无需象thrift那样生成目标语 ...
- Java通过ODBC链接数据库并遍历结果的一个问题
上一篇文章谈到怎么连接Oracle数据库,其实通过ODBC也差不多,只是driver要换成JdbcOdbcDriver.配置文件如下: driver=sun.jdbc.odbc.JdbcOdbcDri ...
- Codeforces Round #380(div 2)
A. 题意:给你一串字符串(<=100),将ogo ogogo ogogogo ogogogogo……这种全部缩成***,输出缩后的字符串 分析:第一遍扫对于那些go的位置,记录下next[i] ...
- .net项目在linux平台的CI流程(基于Jenkins+mono+jexus)
内容较多,主要分为以下4方面内容: Jenkins的安装部署(centos 7+) .net在linux平台CI流程所需的插件管理&配置 Jenkins配置连接Gitlab(也可使用对应插件连 ...
- TCP协议中的三次握手和四次挥手(图解)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- Mac下Intellij IDEA Console中文是?
Mac下Intellij IDEA Console中文是?,解决办法是在运行时的VM参数配置也加入: -Dfile.encoding=UTF-8
- TypeScript Generics(泛型)
软件工程的一个主要部分就是构建组件,构建的组件不仅需要具有明确的定义和统一的接口,同时也需要组件可复用.支持现有的数据类型和将来添加的数据类型的组件为大型软件系统的开发过程提供很好的灵活性. 在C#和 ...
- <!DOCTYPE html>作用
1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...
- 战神Z7 D2安装黑苹果OS X El Capitan 10.11.2
安装之初状态:两块硬盘,都是MBR格式分区,一块是机械硬盘,安装了WIN7 32位和Linux,一块是SSD,安装的是WIN7 64位与WIN10 64位以前玩过Mavericks,安装的就是在硬盘的 ...
- log4Net控制台输出
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...