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 subdir2subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1subdir2contains 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的更多相关文章

  1. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  2. [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  3. 【LeetCode】388. Longest Absolute File Path 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...

  4. Leetcode: Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  5. Leetcode算法比赛----Longest Absolute File Path

    问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...

  6. 【leetcode】388. Longest Absolute File Path

    题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...

  7. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  8. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. 388. Longest Absolute File Path

    就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...

随机推荐

  1. Cannot resolve symbol ‘Component’ & Cannot resolve symbol ‘PropTypes’

    import React, { Component, PropTypes } from 'react' 报错:Cannot resolve symbol 'Component' Cannot reso ...

  2. swagger学习2

    转:http://blog.csdn.net/fansunion/article/details/51923720 写的非常好,非常详细,推荐!!!! 最常用的5个注解 @Api:修饰整个类,描述Co ...

  3. ViewData和ViewBag的那些事

    既然结论是“共享着相同的数据”,那我们就证实一下吧. 看来结论是正确的. 去查看定义,发现他们的类型是不一样的,ViewData是ViewDataDictionary,ViewBag是dynamic. ...

  4. PowerShell官方文档

    PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...

  5. JSON.parse() 和 JSON.stringify()使用

    1.parse()是用于从一个字符串中解析出json对象 定义一个字符串:var str = '{"name":"superman","age&quo ...

  6. nodejs是用来做什么的?

    有些人说“这是一种通过javascript语言开发web服务端的东西”.更直白的可以理解为:node.js有非阻se塞,事件驱动/O等特性,从而让高并发(high concurrency)在的轮询和c ...

  7. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  8. USACO_1.1_Greedy_Gift_Givers_(模拟+水题)

    描述 http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=gift1 给出不超过$10$个人,每个人拿出一定数量的钱平分给特定的人,求最后每个人 ...

  9. Settings点击Location(位置)后右上角的开关button不会消失

    MT8121/8382平台: 前几天又遇到一个源码的bug.在10寸平板上,进入设置界面,点击Location(位置)项,右上角Title处会显示一个开关button,用来开关定位服务.但点完Loca ...

  10. Linux下的Backlight子系统(一)【转】

    转自:http://blog.csdn.net/weiqing1981127/article/details/8511676 版权所有,转载必须说明转自 http://my.csdn.net/weiq ...