my c++ solution


  • 0
    H

    @my code

    class Solution {
    public:
        int lengthLongestPath(string input) {
            int N = input.size();
            int result = 0;
                        //////path length
            std::stack<int > cur_search_path;
            ////paths total length
            int current_length = 0;
    
            bool is_file = false, is_file_dot = false;
                    //path starting position, end position, current path depth
            int i =  0, j1 = 0,  j2 = 0, depth = 0;
            int count_t = -1;
            while( i < N){
                char c = input[i];
                switch(c){
                    case '\n':
                        if(is_file){
                            int new_length = current_length; 
                            new_length += cur_search_path.size();//for counting slash '/'
                            new_length += i - j1;
                            if(new_length > result) result = new_length;
                            j2 = j1;
                            is_file = false;
                        }else{
                            ////a new path is found
                            j2 = i;
                        }
                        ////expecting \t now
                        count_t = 0;
                    break;
                    
                    case '\t': if(count_t >=0 ) ++count_t;    break;
                    
                    case '.': is_file_dot = true;    break;
                    
                    default:
                        if(is_file_dot){
                            is_file = true; is_file_dot = false;
                        }
                        if(count_t >=0 ){
                            if(count_t == depth + 1){
                                if(j2 > j1){
                                    ///a new sub directory
                                    cur_search_path.push(  j2-j1 );
                                    depth += 1;
                                    current_length += j2 - j1;
                                }
                            }else if(count_t <= depth){
                                while(count_t < depth){
                                    current_length -= cur_search_path.top();
                                    cur_search_path.pop();
                                    --depth;
                                }
                            }
                            j1 = i;
                            count_t = -1;
                        }
                    break;
                }
                ++i;
            }
    
            if(is_file){
                int new_length = current_length; 
                new_length += cur_search_path.size();
                new_length += N - j1;
                if(new_length > result) result = new_length;
            }
            return result;
        }
    };
    

Log in to reply
 

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