C++ solution: use map and composite design patten


  • 0
    S
    class FileSystem {
    public:
        FileSystem() 
        {
            
        }
       
        vector<string> ls(string path) 
        {
            if(path.size() <= 1)
            {
                vector<string> res;
                for(const auto & ele : sub_file_system)
                    res.push_back(ele.first);
                return res;
            }
            
            size_t next_pos = path.find('/', 1);
            
            string cur_dir;
            string next_path;
            
            if(next_pos == string::npos)
            {
                cur_dir = path.substr(1);
            }
            else
            {
                cur_dir = path.substr(1, next_pos-1);
                next_path = path.substr(next_pos);
            }
           
            if(sub_file_system.count(cur_dir) && sub_file_system[cur_dir].is_file)
                return {cur_dir};
            else
                return sub_file_system[cur_dir].ls(next_path);
            
        }
        
        void mkdir(string path) 
        {
            if(path.size() <= 1)
                return;
            
            size_t next_pos = path.find('/', 1);
            
            string cur_dir;
            string next_path;
            
            if(next_pos == string::npos)
            {
                cur_dir = path.substr(1);
            }
            else
            {
                cur_dir = path.substr(1, next_pos-1);
                next_path = path.substr(next_pos);
            }
            
            sub_file_system[cur_dir].mkdir(next_path);
        }
        
        void addContentToFile(string filePath, string content) 
        {
            if(filePath.find('/', 1) == string::npos)
            {
                string file_name = filePath.substr(1);
                sub_file_system[file_name].is_file = true;
                sub_file_system[file_name].file_content += content;
                return;
            }
            
            size_t next_pos = filePath.find('/', 1);
            string cur_dir = filePath.substr(1, next_pos-1);
            string next_path = filePath.substr(next_pos);
            
            sub_file_system[cur_dir].addContentToFile(next_path, content);
            
        }
        
        string readContentFromFile(string filePath) 
        {
            if(filePath.find('/', 1) == string::npos)
            {
                string file_name = filePath.substr(1);
                return sub_file_system[file_name].file_content;
            }
            
            size_t next_pos = filePath.find('/', 1);
            string cur_dir = filePath.substr(1, next_pos-1);
            string next_path = filePath.substr(next_pos);
           
            return sub_file_system[cur_dir].readContentFromFile(next_path);
        }
        
    private:
    
        bool is_file = false;
        string file_content;
        map<string, FileSystem> sub_file_system;
    
    };
    
    

Log in to reply
 

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