【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 ...
随机推荐
- C++的命名空间
作用:防止类,函数,变量等之间重名,比如在代码合并的时候 假如两个头文件中均定义了类Cal,而调用程序同时包含了两个头文件,当在定义Cal c时,程序会报类型重定义的错误.这种问题可以通过命名空间来解 ...
- css3 实现可以中英切换的导航条
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- 如何快速使用YOLO3进行目标检测
本文目的:介绍一篇YOLO3的Keras实现项目,便于快速了解如何使用预训练的YOLOv3,来对新图像进行目标检测. 本文使用的是Github上一位大神训练的YOLO3开源的项目.这个项目提供了很多使 ...
- 【TCP】四次握手原因 / TIME_WAIT作用
为什么建立TCP连接需要三次握手? 原因:为了应对网络中存在的延迟的重复数组的问题 例子: 假设client发起连接的连接请求报文段在网络中没有丢失,而是在某个网络节点长时间滞留了,导致延迟到达ser ...
- SVN server 服务端修改端口号
SVN server 服务端修改端口号 在实际使用中可能当安装svn server 服务后,发现与后续其他程序端口冲突, 1.打开svn server 管理界面->操作->properti ...
- 调用windows的复制文件对话框
function CopyFileDir(sDirName: String; sToDirName: String): Boolean; var fo: TSHFILEOPSTRUCT; begin ...
- 如何做LR自动关联和手动关联?
一.什么时候需要关联 1.关联的含义 关联的含义A(correlation):在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服务器所响应的内容 ...
- poi各种jar包作用和导入
poi各种jar包作用和导入 目前POI的最新发布版本是poi-bin-3.17-20170915. 下载地址: Apache POI - Download Release Artifacts ht ...
- java线程池监控
原因 最近在完善公司的基础发布平台的时候,使用到了一线程去做一些异步的事情,在开发环境和测试环境验证没有任何问题,但是在程序在生产运行一段时间后,发现没有得到自己想要的结果,为此开始了漫长的排查bug ...
- JavaScript中 函数的创建和调用
基础概念:定义函数的方式 一般定义函数有两种方式: 1:函数的声明 2:函数表达式 参考资料:https://blog.csdn.net/xixiruyiruyi/article/de ...