Leetcode Longest Absolute File Path
来源:互联网 发布:台湾美女网络走红 编辑:程序博客网 时间:2024/06/10 15:49
题意:给出先序遍历的所有文件路径,求出其中字符串长度最大的文件路径。
思路:问题的关键是记录从根目录到文件的路径长度,这里使用了递推的方式,记录到叶子结点的路径上,每一个 结点的字符串长度。
class Solution {public: int lengthLongestPath(string input) { vector<int> level(200, 0); int templevel = 0; string temps; int re = 0; for(int i = 0; i < input.length(); ++ i) { templevel = 0; bool isFile = false; temps = ""; while(input[i] != '\n' && i < input.length()) { if(input[i] == '\t') templevel ++; else temps += input[i]; if(input[i] == '.') isFile = true; i ++; } //cout << templevel << " " << temps << endl; //initialize the root file if(templevel == 0) { level[templevel] = temps.length(); if(isFile) re = temps.length(); continue; } //cout << "isFile" << isFile << endl; if(isFile) { if(re < level[templevel - 1] + temps.length() + 1) re = level[templevel - 1] + temps.length() + 1; // cout << level[templevel - 1] + temps.length() + 1; } else { level[templevel] = level[templevel - 1] + temps.length() + 1; //cout << level[templevel] << endl; } } return re; }};
转义字符虽然显示两个字符,但是实际只占一个字符的存储空间。
这题本来想用并查集做,将所有路径都记录下来,但是只要最大值的话,没有必要全部记录。这题本质上还是一道搜索题。
0 0
- leetcode:Longest Absolute File Path
- 【Leetcode】Longest Absolute File Path
- Leetcode Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- leetcode -- 388. Longest Absolute File Path
- 388. [LeetCode]Longest Absolute File Path
- LeetCode #388: Longest Absolute File Path
- [Leetcode]388. Longest Absolute File Path
- [leetcode-388]Longest Absolute File Path
- 【LeetCode】 388. Longest Absolute File Path
- [LeetCode]388. Longest Absolute File Path
- LeetCode 388. Longest Absolute File Path
- [leetcode]388. Longest Absolute File Path
- Leetcode 388 Longest Absolute File Path
- Longest Absolute File Path
- Longest Absolute File Path
- 153.Longest Absolute File Path
- mysql 协议的ping命令包及解析
- 看不见的城市 - 笔记
- Fiddler对手机app抓包
- [POJ2947]Widget Factory(高斯消元)
- luogu2053 [SCOI2007]修车
- Leetcode Longest Absolute File Path
- 学习规划
- TQ2440开发板学习纪实(10)--- 实现多任务处理,最简单OS模型
- 171. Excel Sheet Column Number
- Spark DataSet介绍
- IntelliJ IDEA中开启断言的方法
- java 浮点类型精度不准的问题(待测试)
- 学习运维——输入输出重定向、vim
- 基础11