388 Longest Absolute File Path 最长的绝对文件路径

详见:https://leetcode.com/problems/longest-absolute-file-path/description/

C++:

class Solution {
public:
    int lengthLongestPath(string input) {
        int res = 0, n = input.size(), level = 0;
        unordered_map<int, int> m {{0, 0}};
        for (int i = 0; i < n; ++i) 
        {
            int start = i;
            while (i < n && input[i] != '\n' && input[i] != '\t')
            {
                ++i;
            }
            if (i >= n || input[i] == '\n')
            {
                string t = input.substr(start, i - start);
                if (t.find('.') != string::npos) 
                {
                    res = max(res, m[level] + (int)t.size());
                } 
                else 
                {
                    ++level;
                    m[level] = m[level - 1] + (int)t.size() + 1;
                }
                level = 0;
            }
            else
            {
                ++level;
            }
        }
        return res;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5806493.html

posted on 2018-04-15 20:45  lina2014  阅读(118)  评论(0编辑  收藏  举报

导航