当前位置:网站首页>Golang force buckle leetcode 388 The longest absolute path of the file
Golang force buckle leetcode 388 The longest absolute path of the file
2022-04-21 23:15:00 【cheems~】
388. The longest absolute path to the file
388. The longest absolute path to the file
388. The longest absolute path to the file
Answer key
subject : Give you a string dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext
It represents a directory
dir
* subdir1
* * file1.ext
* * subsubdir1
* subdir2
* * subsubdir2
* * * file2.ext
How long is the longest path in the directory
dir/subdir2/subsubdir2/file2.ext return 32
Ideas :
1. The length of the upper level directory name at the same depth is the same
2. Record the upper level directory name , use map save
3.\t The number of is the depth
4.\t To \n Between them is the directory or file name
5. Traversal string , Traverse while saving map
Code
func lengthLongestPath(input string) int {
//route key-> depth val-> The length of the directory
route, n, ans := make(map[int]int), len(input), 0
for i := 0; i < n; i++ {
isFile, depth, cnt := false, 0, 0
// Detect the depth of the current name
for ; i < n && input[i] == '\t'; i++ {
depth++
}
// Check the length of the current name
for ; i < n && input[i] != '\n'; i++ {
if input[i] == '.' {
isFile = true
}
cnt++
}
// Storage map
route[depth] = cnt
sum := 0
// If it's a file
if isFile {
// Count the length of absolute path
for j := 0; j <= depth; j++ {
sum += route[j]
}
//sum+depth Add depth The reason is the absolute path :a/b.txt Medium /
ans = max(ans, sum+depth)
}
}
return ans
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
版权声明
本文为[cheems~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212307301743.html
边栏推荐
- 8.4 control of wheel motion of mobile robot in rodf robot modeling
- 从零开始自制实现WebServer(十六)---- 学习新工具CMake自动编写MakeFile 分门别类整理源文件心情愉悦
- Go language self-study series | golang init function
- [MQ] starting from scratch to realize mq-01-start of producers and consumers
- golang力扣leetcode 2246.相邻字符不同的最长路径
- 【ACM】46. Full Permutation (1. Here, the previous elements need to be used for permutation, so StartIndex is not used (only for combination and division); 2. Pay attention to whether the elements in t
- P1027 [NOIP2001 提高组] Car 的旅行路线 (图 最短路)
- Precautions for self built VNC software
- 痞子衡嵌入式:聊聊系统看门狗WDOG1在i.MXRT1xxx系统启动中的应用及影响
- Selection and evolution of microservices under cloud native architecture
猜你喜欢

云原生架构下的微服务选型和演进

【ACM】46. Full Permutation (1. Here, the previous elements need to be used for permutation, so StartIndex is not used (only for combination and division); 2. Pay attention to whether the elements in t

Its and LPI interrupt of GIC spec 5

【速卖通代运营】2022跨境电商怎么做?速卖通今年重点要做三件事

瑞芯微芯片AI部分开发记录 第一节 《PC端环境搭建1》

Self made webserver from scratch (XVI) -- learn a new tool cmake to automatically write makefile and sort out source files by categories. Feel happy

Implementation of neural network attention mechanism by pytoch code

GIC spec之ITS和LPI中断5

痞子衡嵌入式:聊聊系统看门狗WDOG1在i.MXRT1xxx系统启动中的应用及影响

thinkphp开发卡密社区系统
随机推荐
Kubernetes---Secret配置管理
MySQL problem solving_ Multi table joint query_ Please take out the corresponding data and output it in ascending order of accuracy.
技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关
golang力扣leetcode 2245.转角路径的乘积中最多能有几个尾随零
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
2022-04-22日报:基于Transformer的新型人脸属性编辑框架TransEditor
Discussion on digital business of traditional enterprises - Digital Architecture Design (5)
Basic concepts of audio and video and a simple introduction to ffmpeg
叹为观止,4款惊喜满满的高质量软件,使用起来倍感舒适
(三)瑞芯微rk3568 ssh 替换 dropbear
VOS如何设置盲区
VOS6. 0 installation and source code command
自定义模板问题求助,自动添加时间日期
MySQL 第5章 MySQL表数据的增删改查
VOS6.0安装及源码命令
golang力扣leetcode 2246.相邻字符不同的最长路径
3. MySQL workbench performs addition, deletion, modification and query on the table
golang力扣leetcode 386.字典序排数
手机APP游戏/软件/资源下载站/软件盒子源码
大厂面经合集,这些知识点你会吗