【leetcode】388. 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.extThe directory
dircontains an empty sub-directorysubdir1and a sub-directorysubdir2containing a filefile.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.extThe directory
dircontains two sub-directoriessubdir1andsubdir2.subdir1contains a filefile1.extand an empty second-level sub-directorysubsubdir1.subdir2contains a second-level sub-directorysubsubdir2containing a filefile2.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 is32(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)wherenis the size of the input string.Notice that
a/aa/aaa/file1.txtis not the longest file path, if there is another pathaaaaaaaaaaaaaaaaaaaaa/sth.png.
解题思路:我的方法很简单,首先把input按'\n'分割,接下来判断分割后的每一个子串前缀有几个'\t',并以'\t'的个数作为key值将子串存入字典中,而这个子串的上一级目录是(key-1)在字典中的最后一个元素。遍历所有的子串后即可求得最大长度。
代码如下:
class Solution(object):
def lengthLongestPath(self, input):
"""
:type input: str
:rtype: int
"""
#print input
dic = {}
dic[0] = ['']
res = 0
if input.count('.') < 1:
return 0
itemlist = input.split('\n')
for i in itemlist:
count = i.count('\t') + 1
if count not in dic:
dic[count] = [dic[count-1][-1] + '/' + i.replace('\t','')]
else:
dic[count].append(dic[count-1][-1]+ '/'+i.replace('\t',''))
#print len(dic[count][-1])-1,dic[count][-1]
if dic[count][-1].count('.') >= 1:
res = max(res,len(dic[count][-1])-1)
#print dic
return res
【leetcode】388. Longest Absolute File Path的更多相关文章
- 【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 ...
- 388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...
- 388 Longest Absolute File Path 最长的绝对文件路径
详见:https://leetcode.com/problems/longest-absolute-file-path/description/ C++: class Solution { publi ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- [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 ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
随机推荐
- mysql中limit 和 limit 与 offset 的用法(效果相同,用法不通过)
例1,假设数据库表student存在13条数据. 代码示例: 语句1:select * from student limit 9,4 语句2:slect * from student limit 4 ...
- eclipse中服务器找不到项目怎么解决
在我们运行项目前,都需要将项目部署到tomcat上,但是有时我们会遇到这种情况:项目明明存在,但是eclipse中tomcat的add and remove找不到项目,无法部署,那么这个问题该如何解决 ...
- 虚拟机(JVM)如何加载类
首先JVM加载类的一般流程分三步: 加载 链接 初始化 那么是否全部Java类都是这样三步走的方式加载呢?我们可以从Java的数据类型去出发.Java分基本类型和引用类型.其中按照面向对象的特性,一切 ...
- 【leetcode】316. Remove Duplicate Letters
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- zk 后台调用前台点击事件
Events.echoEvent(new Event(Events.ON_CLICK, view.getFellow("showYjdkhMessage"))); showYjdk ...
- css float 浮动
CSS Float(浮动) 什么是 CSS Float(浮动)?大理石平台价格 CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. Float(浮动),往往是用于图像, ...
- 使用JAVA如何对图片进行格式检查以及安全检查处理
一.通常情况下,验证一个文件是否图片,可以通过以下三种方式: 1).判断文件的扩展名是否是要求的图片扩展名 这种判断是用得比较多的一种方式,不过这种方式非常的不妥,别人稍微的把一个不是图片的文件的扩展 ...
- AcWing 214. Devu和鲜花 (容斥)打卡
Devu有N个盒子,第i个盒子中有AiAi枝花. 同一个盒子内的花颜色相同,不同盒子内的花颜色不同. Devu要从这些盒子中选出M枝花组成一束,求共有多少种方案. 若两束花每种颜色的花的数量都相同,则 ...
- SQL必知必会——创建和操纵表(十七)
1.创建表 一般有两种创建表的方法: 多数DBMS都具有交互式创建和管理数据库表的工具表也可以直接用SQL语句操纵1.1.表创建基础 CREATE TABLE products( prod_id,CH ...
- java中 抽象类和接口的区别
一. 什么是抽象类及什么是抽象方法 抽象方法是一种特殊的方法:他只有声明,而没有具体实现,抽象方法的声明格式为: abstract void funName(); 抽象方法必须用 abstract 修 ...