题目来源
给一个string,求最长路径。
主要在于记录当前是第几层,前几层一共多长长度,是否是文件之类的。
class Solution {
public:
int lengthLongestPath(string input) {
int n = input.size(), LongestPath = 0;
vector<int> levelLen;
levelLen.push_back(0);
for (int i=0; i<n; i++) {
int count = 0, curLevel = 1;
bool isFile = false;
while (input[i] == '\t') {
curLevel++;
i++;
}
while (input[i] != '\n' && i < n) {
if (input[i] == '.')
isFile = true;
count++;
i++;
}
if (isFile)
LongestPath = max(LongestPath, levelLen[curLevel-1] + count);
else {
if (levelLen.size() > curLevel)
levelLen[curLevel] = levelLen[curLevel-1] + count + 1;
else
levelLen.push_back(levelLen[curLevel-1] + count + 1);
}
}
return LongestPath;
}
};