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表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- Codeforces 931.D Peculiar apple-tree
D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Different Integers 牛客多校第一场只会签到题
Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...
- idea设置文件的编码格式
在打开某些类时会发现注释是乱码的,该如何解决idea的文件乱码呢?这就需要设置这个文件的合适编码格式: idea设置文件编码的两种方式分别如下: 第一种方式点击idea的右下角的图标如下图所示: 第二 ...
- 7月23号day15总结
数据清洗完成之后开始编写前端,通过spring框架将清洗后数据库中的数据显示在页面中. 框架的搭建和js的使用都在学习阶段,
- CSS3学习笔记之loading动画
效果截图: HTML代码: <div class="divBox"> <div class="loader"> <div clas ...
- UVa11988 Broken Keyboard (a.k.a. Beiju Text)
题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...
- [bzoj4515][Sdoi2016]游戏-树链剖分+李超线段树
Brief Description Alice 和 Bob 在玩一个游戏. 游戏在一棵有 n 个点的树上进行.最初,每个点上都只有一个数字,那个数字是 123456789123456789. 有时,A ...
- Shell Script Basics
https://developer.apple.com/library/mac/documentation/OpenSource/Conceptual/ShellScripting/shell_scr ...
- 【MT8382/8121】为MTK的工厂测试添加测试项
摘要: 本文介绍添加MTK工厂测试项的步骤及调试技巧. 纲要: 1. 描述添加MTK工厂测试项的步骤 2.调试小技巧 1. 描述添加MTK工厂测试项的步骤 以添加红外测试为例: 1. mediatek ...
- 哪些情况会导致OOM
1. 堆溢出 java堆用于存储对象实例,只要不断地创建对象,并且这些对象不会被回收(什么情况对象不会被回收呢?如:由于GC Root到对象之间有可达路径,所以垃圾回收机制不会清除这些对象),那么,当 ...