当前位置:网站首页>Daily question (2022-04-20) - the longest absolute path of the file
Daily question (2022-04-20) - the longest absolute path of the file
2022-04-21 09:44:00 【Fill your head with water】
Catalog
388. The longest absolute path to the file
Title Description :
Suppose you have a file system that stores files and directories at the same time .
The following figure shows an example of a file system :
There will be dir As the only directory in the root directory .
dir Contains two subdirectories subdir1 and subdir2 .subdir1 Include files file1.ext And subdirectories subsubdir1;subdir2 Include subdirectories subsubdir2, This subdirectory contains files file2.ext .
In text format , As shown below (* Represents a tab ):dir * subdir1 * * file1.ext * * subsubdir1 * subdir2 * * subsubdir2 * * * file2.extIn case of code representation , The above file system can be written as
“dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext”
‘\n’ and ‘\t’ Line breaks and tabs, respectively .Each file and folder in the file system has a unique Absolute path , That is, you must open it to reach the file / Directory order of directory location , For all paths ‘/’ Connect .
In the example above , Point to file2.ext The absolute path of "dir/subdir2/subsubdir2/file2.ext" .
Each directory name consists of the letters 、 Numbers and / Or spaces , Each file name follows name.extension The format of , Where the name and extension consist of the letters 、 Numbers and / Or spaces .
requirement :
Give a string representing the file system in the above format input , Return to the file system The longest absolute path to a file The length of . If there are no files in the system , return 0.
Example 1:
Input :input = “dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext”
Output :20
explain : Only one file , The absolute path is “dir/subdir2/file.ext” , The length of the path 20
Example 2:
Input :input = “file1.txt\nfile2.txt\nlongfile.txt”
Output :12
explain : There is... In the root directory 3 File .
Because the absolute path of anything in the root directory is just the name itself , So the answer is “longfile.txt” , The path length is 12
Example 3:
Input :input = “a”
Output :0
explain : No files exist
Be careful :
- 1 <= input.length <= 104
- input May contain lowercase or uppercase letters , A line break ‘\n’, A tab ‘\t’, One point ‘.’, A space ’ ', And number .
Ideas :
The file system is like a tree

Positive solution :
func lengthLongestPath(input string) int {
if input == "" || len(input) == 0 || !strings.Contains(input, ".") {
return 0
}
// The string is equivalent to... Of the tree dfs Depth-first search
// dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext
// [dir \tsubdir1 \t\tfile1.ext \t\tsubsubdir1 \tsubdir2 \t\tsubsubdir2 \t\t\tfile2.ext]
words := strings.Split(input, "\n")
// pathLens[i] Deposit The first i The path length of the last element of the hierarchy
pathLens := make([]int, len(words)+1)
// The lowest level is 1, In order to unify dp operation ( See the following circular body for details ), take pathLens[0]=-1
pathLens[0] = -1
var ans int
// From left to right ,dp
for _, word := range words {
// Hierarchical computing
// \t Number Number of representative floors
// Be careful :
// \n and \t All represent 1 position Not two
// strings.LastIndex("\t\tsubdir1","\t") The returned subscript is 1
level := strings.LastIndex(word, string('\t')) + 1+1
// Calculate the length of the file name
// word :"\tsubdir1"
// len(word) : 8
// level:0
// nameLen:9
nameLen := len(word) - (level-1)
// word The parent folder of must be currently level-1 The last of the hierarchy ,
// therefore pathLens[level-1] Is the path length of the parent folder
// This word It must be the last one at this level ,
// Therefore, refresh is required pathLens[level],+1 Because we need to add one '\'
pathLens[level] = pathLens[level-1] + 1 + nameLen
// If it's a file , Refresh with path length ans
if strings.Contains(word, ".") {
ans = int(math.Max(float64(ans), float64(pathLens[level])))
}
}
return ans
}
Submit results :

版权声明
本文为[Fill your head with water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210940157390.html
边栏推荐
- Multithreaded small copy set (New Edition 2)
- Grid layout -- grid
- 字符串匹配 KMP BF
- 广州期货交易所的产品有哪些呢?
- You are using pip version 20.2.3; however, version 22.0.4 is available. You should consider
- JS - complete Gobang layout in 70 lines
- 常用文本处理命令
- Ansible_03_高级playbook
- 刷题记录(牛客MySQL)
- [Yugong series] wechat applet - API related function case of map use in April 2022
猜你喜欢

Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022

Fudan University - University of Washington EMBA Alumni: Turning "her power" into "our power"

VR panorama is suitable for those industries

2022制冷与空调设备运行操作考试题模拟考试题库及答案

又涨了,4月全国程序员薪资出炉,竟成行业薪资天花板?看完我心动了

2022 refrigeration and air conditioning equipment operation test question simulation test question bank and answers
![[hand in hand to prepare you for the electric game] use the timer interrupt to change the PWM duty cycle](/img/b3/203fdbd700347c531e6567b6972594.png)
[hand in hand to prepare you for the electric game] use the timer interrupt to change the PWM duty cycle

The display problem of gltf model with transparent map

Generate training set and verification set (Yolo) by using existing label files

事务的隔离级别与MVCC
随机推荐
Sixtool, which is launched in the whole network, is a multi-functional and multi-in-one generation hanging assistant. It supports BiliBili, movement steps and other functions. The operating version ca
报告解读下载 | 首份《中国数据库行业分析报告》重磅发布!
gunicorn使用----服务端项目部署
redis部署与使用
刷题记录(leetcode)
响应式布局实现ghost博客首页静态页面
刷题记录(牛客MySQL)
多线程---解析无锁队列的原理与实现
【DL-图像分类】
C语言简单的【栈和队列】(括号匹配问题)
2022年制冷与空调设备运行操作考试试题模拟考试平台操作
[hand in hand to prepare you for the video game] monochrome block recognition (based on openmv)
CANoe:Vector Tool Platform是什么
Write table of MySQL Foundation (create table)
[Yugong series] wechat applet - API related function case of map use in April 2022
Fudan University - University of Washington EMBA Alumni: Turning "her power" into "our power"
[cloud resident co creation] database principle and application course of Huawei cloud database 11. Database system control
CentOS下Docker中安装redis
利用已有的标签文件生成训练集和验证集(YOLO)
Generate training set and verification set (Yolo) by using existing label files
