leetcode 388.Lonest Absolute File Path

要求寻找的是最长的长度的,那么考虑文件夹和文件的存在的不同形式的,利用当前所存在的层数和对应的长度之间做一个映射关系的并且用于计算总的长度关系的。

class Solution {
public:
    int lengthLongestPath(string input) {
        int res=0,level=0;
        map<int,int> m{{0,0}};
        int n=input.size();
        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 temp=input.substr(start,i-start);
                if(temp.find('.')!=string::npos){
                    res=max(res,m[level]+(int)temp.size());
                }else{
                    level++;
                    m[level]=m[level-1]+(int)temp.size()+1;
                }
                level=0;
            }
            else{
                level++;
            }
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/newnoobbird/p/9628524.html