我的力扣主页:
https://leetcode-cn.com/u/cutesnake/
4.20
388中等
https://leetcode-cn.com/problems/longest-absolute-file-path/
乍一看简单,仔细一想不会做。本来想用遍历,看了答案知道用哈希表,但还是太难办,写了一个小时,wa一次,有点理解屎山是如何堆成的了……
class Solution {
public:
int lengthLongestPath(string input) {
int res = 0, curLen = 0, subCnt = 0, n = input.size();
bool isFile = 0;
unordered_map<int, int> subNum;
for (int i = 0; i < n; ++i) {
char& ch = input[i];
if (ch == '\n') {
isFile = 0;
subNum[subCnt] = curLen;
subCnt = 0;
continue;
}
if (ch == '\t') {
++subCnt;
} else {
if (i && (input[i-1] == '\t' || (input[i-1] == '\n' && subCnt == 0))) curLen = subNum[subCnt-1];
if (ch == '.') isFile = 1;
++curLen;
if (isFile) res = max(res, curLen + subCnt);
}
}
return res;
}
};