程式語言 - LeetCode - C++ - 388. Longest Absolute File Path



題目:


解答:

class Solution {
public:
    int lengthLongestPath(string input) {
        int max_len = 0;
        string word;
        stringstream ss(input);
        unordered_map<int, int> len;

        while (getline(ss, word, '\n')) {
            int depth = 0;

            while (word[depth] == '\t') {
                depth += 1;
            }

            string name = word.substr(depth);
            if (name.find('.') != string::npos) {
                max_len = max(max_len, static_cast<int>(len[depth] + name.length()));
            }
            else {
                len[depth + 1] = len[depth] + sizeof('/') + name.length();
            }
        }

        return max_len;
    }
};