C++/Longest Absolute File Path


  • 0
    X
    class Solution {
    public:
        int lengthLongestPath(string input) {
            vector<int> pool;
            int length=0,level=0;
            bool file = false;
            int max = 0;
            for(int i=0 ; i<input.size() ; i++ ){
                char c = input[i];
                if( '\n' == c){
                    if( level>0 )
                        length += pool[level-1];
                    if( file ){
                        max = max>length?max:length;
                    }else if( level >= pool.size() ){
                        pool.push_back( length+1 );
                    }else{
                        pool[level] = length+1;
                    }
                    level = 0;
                    length = 0;
                    file = false;
                }else if( '\t' == c){
                    level ++;
                }else if( '.' == c ){
                    length++;
                    file = true;
                }else{
                    length++;
                }
            }
    
            if( file ){
                if( level>0 )
                    length += pool[level-1];
                max = max>length?max:length;
            }
            return max;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.