美文网首页
Longest Absolute File Path

Longest Absolute File Path

作者: 我叫胆小我喜欢小心 | 来源:发表于2017-05-11 20:06 被阅读7次

题目来源
给一个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;
    }
};

相关文章

网友评论

      本文标题:Longest Absolute File Path

      本文链接:https://www.haomeiwen.com/subject/fchatxtx.html