Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2contains a second-level sub-directory subsubdir2 containing a file file2.ext.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is"dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.
Note:
- The name of a file contains at least a
.and an extension. - The name of a directory or sub-directory will not contain a
..
Time complexity required: O(n) where n is the size of the input string.
Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.
思路:将input根据'\n'分割,根据开始的'\t'数量判断深度。用栈来统计当前的路径长度。
这个题学到了两个点:1是如何分割字符串,2是如何判断一个字符串是否含有某个子串。
class Solution {
public:
vector<string> split(istringstream& input, char delim) {
string item;
vector<string> res;
while (std::getline(input, item, delim)) {
res.push_back(item);
}
return res;
}
int countTb(string path) {
int res = ;
for (int i = , n = path.size(); path[i] == '\t' && i < n; i++)
res++;
return res;
}
int lengthLongestPath(string input) {
std::istringstream iss(input);
vector<string> paths = split(iss, '\n');
stack<int> res;
int maxLen = ;
for (string path : paths) {
int level = countTb(path);
int lengthInc = path.size() - level + ;
while (res.size() >= level) res.pop();
if (res.empty()) res.push(lengthInc - );
else res.push(res.top() + lengthInc);
if (path.find('.') != std::string::npos)
maxLen = std::max(maxLen, res.top());
}
return maxLen;
}
};
Longest Absolute File Path -- LeetCode的更多相关文章
- [LeetCode] 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\n\tsub ...
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- Leetcode: 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 ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
- [Swift]LeetCode388. 文件的最长绝对路径 | 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 ...
- 388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- C++——设计模式说明
一.设计模式6大原则 名称 解释0.单一职责原则(SRP) 就一个类而言,应该仅有一个引起它变化的原因.一."开放-封闭"原则(OCP) 在软件设计模式中,这种不能修改,但可以扩展 ...
- 【luogu 1439 最长公共子序列】
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...
- GDB使用小记
By francis_hao Nov 7,2016 记录GDB常用功能. GDB可以让你查看一个程序在运行时其内部发生了什么,或者当一个程序崩溃时发生了什么(通过使用GDB查看core dum ...
- HDU2732:Leapin' Lizards(最大流)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 4417 划分树写法
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- 如何取消PPT中的动画效果
幻灯片放映——>设置放映式——>勾选放映时不加动画 (office2007)
- .NET之特性和属性(转)
1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...
- oracle有关游标的知识
一:前言 今天我自己第二次写游标,我擦,觉得自己在数据库方面需要很大的提高啊.今天遇到三个问题,第一个是oracle数据库中的数据拆分的问题,这个我用regexp_substr来进行解决,第二个问题就 ...
- HDU1099---数学 | 思维
hdu 1099 Lottery题意:1~n编号的彩票,要买全,等概率条件下平均要买几张.已经买了m张时,买中剩下的概率为1-m/n,则要买的张数为1/(1-m/n)n=2,s=1+1/(1-1/2) ...
- salt-api的使用
curl -k https://192.168.74.129:8006/ -H "Accept: application/x-yaml" -H "X-Auth-Token ...