当前位置:网站首页>2022.4.20-----leetcode.388
2022.4.20-----leetcode.388
2022-04-23 01:44:00 【路Lu727】
public static int lengthLongestPath(String input) {
String[] dirs=input.split("\n");//分割
Deque<Integer> stk=new ArrayDeque<>();
int cur=0;//当前路径长度
int max=0;//最大路径长度
for (String dir : dirs) {
int layer=0;//有几个'\t'代表该目录在第几层
int i;
for (i = 0; dir.charAt(i)=='\t' ; i++) {
layer++;
}
int len=dir.substring(i).length();//该目录名称长度
//根据该目录所处层进行出栈,并更新cur
int n=stk.size();//表示当前在第n-1层
for (int j = 0; j < n-layer; j++) {
cur-=stk.pollLast()+1;
}
stk.offer(len);//添加该目录
cur+=len+1;
//遇到文件记录最大长度
if(dir.contains("."))
max=Math.max(max,cur);
}
if(max==0)//没有文件
return max;
return max-1;//第一个目录前没有'/'
}
版权声明
本文为[路Lu727]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_60466670/article/details/124355722
边栏推荐
猜你喜欢
Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
C语言中如何“指名道姓”的进行初始化
Detonate the bomb (DFS)
VSCODE + PHP Debug + 名字空间指引
Redis implements distributed locks
Abused "architect"!
About how to import C4d animation into lumion
Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
The most understandable life cycle of dependency injection
W801/W800-wifi-socket开发(一)-UDP
随机推荐
42. Use k3det in mmrotate for rotating target detection, MNN deployment and ncnn deployment
[course summary] Hello harmonyos series of courses, take you to zero foundation introduction
批处理多个文件合成一个HEX
JSP basic knowledge summary
VSCODE + PHP Debug + 名字空间指引
计蒜客:数独(DFS)
The most easy to understand service container and scope of dependency injection
Itextsharp displays Chinese fonts
PID精讲
Solve the problem when installing MySQL
[蓝桥杯][2019年第十届真题]外卖店优先级
keil mdk中文乱码,两种解决方法,字体不再难看
mb_substr()、mb_strpos()函数(故事篇)
Abused "architect"!
数字藏品平台入驻 数字藏品平台开发 数字藏品SaaS平台
稳定币是让公链加速死亡或者取代BTC的超级机会?
J-Link RTT使用
When should I write unit tests? What is TDD?
Longest common subsequence (record path version)
LSF的常用使用命令总结