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 ...
随机推荐
- CentOS 6.4 安装思维到图工具TheBrain
最近学习中需要使用思维导图的工具,但是使用的系统是CentOS,在网上找到了一个比较好的思维导图工具:TheBrain,安装完成后还是汉化版的不错啊,由于用的是linux系统,还没有找到合适的截图软件 ...
- C语言和C++中动态申请内存
在C语言和C++的动态内存的使用方法是不同的,在C语言中要使用动态内存要包含一个头文件即 #include<malloc.h> 或者是#include<stdlib.h> ...
- 【bzoj】1026: [SCOI2009]windy数
1026: [SCOI2009]windy数 Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间 ...
- 标签form表单里的属性简单介绍了一下
<form id="form1" name="form1" method="post" action=""> ...
- XSS测试用例与原理讲解
1.<a href="javascript:alert(32)">DIBRG</a>2.<img href="javascript:aler ...
- hdu 2894
刚刚看到这个题感觉挺复杂的~~~因为它还要输出字典序: 很容易知道对于任意的k,第一个输出总是1<<k; 而对于第二个嘛,不管怎么样,前k个元素总是k个0: 然后取前k-1个数,加上0或者 ...
- 思考 Swift 中的 MirrorType 协议
Swift中的反射非常有限,仅允许以只读方式访问元数据的类型子集.或许 Swift 因有严格的类型检验而不需要反射.编译时已知各种类型,便不再需要进行进一步检查或区分.然后大量的 Cocoa API ...
- 【UVA 10369】 Arctic Network (最小生成树)
[题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站 ...
- HANDLE HINSTANCE HWND CWnd CDC
HANDLE HINSTANCE HWND CWnd CDC 句柄: 柄,把柄 例如一个锅的手柄,你只要抓住了它,你就可以很好地操作那个锅,不过很明显你只能通过锅的手柄来做一些诸如炒菜之类的事,你 ...
- 17.1.1.2 Setting the Replication Slave Configuration
17.1.1.2 Setting the Replication Slave Configuration 在一个复制slave, 你必须创建一个唯一的server ID,如果这个没有做,slave设置 ...