当前位置:网站首页>golang力扣leetcode 388.文件的最长绝对路径
golang力扣leetcode 388.文件的最长绝对路径
2022-04-21 23:07:00 【cheems~】
388.文件的最长绝对路径
388.文件的最长绝对路径
题解
题目:给你一个字符串dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext
其表示的是一个目录
dir
* subdir1
* * file1.ext
* * subsubdir1
* subdir2
* * subsubdir2
* * * file2.ext
最目录中的最长路径长多少
dir/subdir2/subsubdir2/file2.ext 返回32
思路:
1.同一深度的上一级目录名长度一样
2.记录上一级目录名,用map存
3.\t的个数就是深度
4.\t到\n之间就是目录或者文件名
5.遍历字符串,边遍历边存map
代码
func lengthLongestPath(input string) int {
//route key->深度 val->目录的长度
route, n, ans := make(map[int]int), len(input), 0
for i := 0; i < n; i++ {
isFile, depth, cnt := false, 0, 0
//检测当前名称的深度
for ; i < n && input[i] == '\t'; i++ {
depth++
}
//检测当前名称的长度
for ; i < n && input[i] != '\n'; i++ {
if input[i] == '.' {
isFile = true
}
cnt++
}
//存储map
route[depth] = cnt
sum := 0
//如果是文件
if isFile {
//统计绝对路径的长度
for j := 0; j <= depth; j++ {
sum += route[j]
}
//sum+depth 加depth的原因是绝对路径:a/b.txt中的/
ans = max(ans, sum+depth)
}
}
return ans
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
版权声明
本文为[cheems~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42956653/article/details/124319466
边栏推荐
- 7.4 introduction to robot kinematics | manipulator kinematics | two wheel differential chassis kinematics | wheel odometer
- Basic concepts of audio and video and a simple introduction to ffmpeg
- Mysql的并发参数调整详解
- 第2章 MySQL数据库的安装
- OS实验三【进程通信】
- 【中南林业科技大学】【陈】第九周作业三角形异常处理
- 技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关
- MySQL problem solving_ Multi table joint query_ Please take out the corresponding data and output it in ascending order of accuracy.
- 成员变量与局部变量的对比
- Fine grained emotion analysis practice
猜你喜欢

Some cold thoughts behind the popularity of microservices, middle office, RPA and low code

2022 electrician (elementary) examination simulated 100 questions and simulated examination

1. MySQL workbench 8.0 installation

6. Example of QT using MySQL

Keil package common chip high-speed download address, including historical version, pay close attention to my quick collection

How does the applet integrate instant messaging with instant messaging

How does IOT platform realize business configuration center

Fine grained emotion analysis practice

第2章 MySQL数据库的安装

大厂面经合集,这些知识点你会吗
随机推荐
[sdoi2012] Chandelier
Why is everything console I can't come out. console. log(depsMap)
Detailed explanation of MySQL concurrency parameter adjustment
One plus two earphone products: charge for 10 minutes and listen to music for 20 hours
循环队列与扩容
成员变量与局部变量的对比
P1027 [NOIP2001 提高组] Car 的旅行路线 (图 最短路)
8.4 control of wheel motion of mobile robot in rodf robot modeling
Informatics Aosai yibentong 1210: factor decomposition | openjudge 1.13 22: factor decomposition
深入理解mysql各种锁
MySQL problem solving_ Multi table joint query_ The operator wants to calculate the average answer volume of users from different schools and different difficulties who participated in the answer. Ple
P1027 [noip2001 improvement group] car's travel route (the shortest path in the diagram)
Teach you to easily solve CSRF Cross Site Request Forgery Attack
1956 college entrance examination mathematics
REM practical development adaptation scheme for mobile web development
Go language self-study series | golang init function
Intelligent Cloud business group (ACG)
Yarn online dynamic resource tuning
【FPGA的基础学习-------OV7725摄像头模块】
Rvb2601 startup process