388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关。
\n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测.
之后的\t表示层数。
思路是如果当前层数多余已经有的层数说明是在go deeper,就继续,否则就要回到他属于的父目录,这个性质用STACK来实现再好不过。。
然后一开始没注意求的是最大长度,所以想的是往里PUSH文件夹的名字,其实因为只需要长度,光PUSH文件夹名字的长度就行了。。
用split("\n")来分,注意\t \n是他妈一个字符,一开始蠢了,以为是2个。。
public class Solution {
public int lengthLongestPath(String input)
{
int res = 0;
if(input.length() == 0) return res;
Stack<Integer> stk = new Stack<Integer>();
stk.push(0);
String[] s = input.split("\n");
for(int i = 0; i < s.length;i++)
{
int level = s[i].lastIndexOf("\t") + 1; //+1 because it starts with 0. -1 means none
int tempLength = 0;
//dir \n \tsubdir1 \n \tsubdir2 \n \t\tfile.ext
while(stk.size()-1 > level) stk.pop();
//father dir length + cur length - number of \t + "/"
tempLength = stk.peek() + s[i].length() - level + 1;
//u can push file into it, it will be poped anyway.
stk.push(tempLength);
// we only look for files' path
if(s[i].contains(".")) res = Math.max(res,tempLength);
}
// no file will return 0
if(res == 0) return 0;
// first dir does not have "/"
return res-1;
}
}
二刷。
特别像DFS,用Stack来记录回溯的位置。
开始用Split按照\n分开,然后开始数\t的数量,代表所在的层数,深于当前层说明继续深入搜索;否则回溯。
当前层的深度就是stk的size,一开始存的string,快做完发现其实不需要,直接存字符的长度就可以。
深度搜索前要把当前的字符长度加入到stk里,如果是dir得 + 1作为代表/的长度;不是的话更新下最长长度,再直接加进去,反正往后要pop出来。(也可以不加,因为下一次肯定要POP出来。。)
Time : O(n)
Space : O(n)
一刷应该是看了答案,二刷自己写的= =所以不如一刷简洁。
public class Solution {
public int lengthLongestPath(String input) {
if (input.length() == 0) return 0;
Stack<Integer> stk = new Stack<>();
String[] paths = input.split("\n");
int tempMax = 0;
int tempStkMax = 0;
for (int i = 0; i < paths.length; i++) {
String s = paths[i];
//System.out.println("Current s is " + s);
int j = 0;
int temp = 1;
while (j < s.length() && s.charAt(j) == '\t') {
temp ++;
j ++;
}
//System.out.println("level is: " + temp + " stk.size() is " + stk.size());
String pathName = s.substring(j);
//System.out.println("pathName : " + pathName);
while (temp <= stk.size()) {
tempStkMax -= stk.pop();
}
//System.out.println("Length of all strings in stk: " + tempStkMax);
int ext = pathName.indexOf(".");
if (ext != -1) {
//stk.add(pathName.length());
//tempStkMax += pathName.length();
tempMax = Math.max(tempMax, tempStkMax + pathName.length());
} else {
stk.add(pathName.length()+1);
tempStkMax += pathName.length()+1;
}
}
return tempMax;
}
}
388. Longest Absolute File Path的更多相关文章
- [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】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- 388 Longest Absolute File Path 最长的绝对文件路径
详见:https://leetcode.com/problems/longest-absolute-file-path/description/ C++: class Solution { publi ...
- [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
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 ...
- [Swift]LeetCode388. 文件的最长绝对路径 | 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 ...
随机推荐
- java中运算符——进度1
Class Demo1{ public static void main(String[] args) { /* 一.逻辑运算法用于连接两个boolean类型的表达式 ...
- 包管理器Bower使用手册之一
包管理器Bower使用手册之一 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.Bower介绍 Bower是一个适合Web应用的包管理器,它擅长前端的 ...
- Manacher 算法-----o(n)回文串算法
回文的含义是:正着看和倒着看相同,如abba和yyxyy Manacher算法基本要点:用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插 ...
- 由tomcat启动想到的
1.batch:批处理文件,表示一批 2.profile:轮廓 3.用户变量和系统变量的关系是什么? 答:点击"我的电脑→属性→高级"标签的"环境变量&quo ...
- uva 10881 - Piotr's Ants
这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...
- iOS开发控制器之间传值的几种小方法
在IOS开发中或面试中,经常会遇到,两个或者多个控制器之间传值的问题 ,总结的集中方法仅供参考! 问题 :将B控制器中的textField 输入内容,传到A控制器中的label上显示出来,如何传值? ...
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
EditView和TextView的用法差不多,只是文字可编辑 小技巧: 设置EditText隐藏键盘 setInputType(0); 设置EditText不被输入法遮盖 getWindow() ...
- Learning WCF 书中的代码示例下载地址
Learning WCF Download Example Code 第一个压缩文件LearningWCF.zip是VS2005创建的项目,不要下载这个. 建议下载VS2008版的,以及Media
- bzoj2209 2329
括号序列的经典做法把(看成1,)看成-1匹配的括号序列即任意前缀和都非负我们先解决静态的问题,给定一段括号序列求最少修改次数我们先找出最大后缀和a和最小前缀和b之间一定可以不相交显然a+|b|个括号是 ...
- JIRA Cannot Start Due to 'unable to clean the cache directory: /opt/jira/plugins/.osgi-plugins/felix'
Symptoms After restarting JIRA, the following error appeared: JIRA Startup Failed You cannot access ...