美文网首页
lt.588 设计内存文件系统

lt.588 设计内存文件系统

作者: 东京的雨不会淋湿首尔 | 来源:发表于2021-08-24 15:32 被阅读0次

    设计一个内存文件系统,模拟以下功能:

    ls: 以字符串的格式输入一个路径。如果它是一个文件的路径,那么函数返回一个列表,仅包含这个文件的名字。如果它是一个文件夹的的路径,那么返回该 文件夹内 的所有文件和子文件夹的名字。你的返回结果(包括文件和子文件夹)应该按字典序排列。

    mkdir:输入一个当前不存在的 文件夹路径 ,你需要根据路径名创建一个新的文件夹。如果有上层文件夹路径不存在,那么你也应该将它们全部创建。这个函数的返回类型为 void 。

    addContentToFile: 输入字符串形式的 文件路径 和 文件内容 。如果文件不存在,你需要创建包含给定文件内容的文件。如果文件已经存在,那么你需要将给定的文件内容 追加 在原本内容的后面。这个函数的返回类型为 void 。

    readContentFromFile: 输入 文件路径 ,以字符串形式返回该文件的 内容 。

    示例:

    输入:
    ["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"]
    [[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]]

    输出:
    [null,[],null,null,["a"],"hello"]

    struct trie {
        bool isFile;
        string content;
        map<string, trie*> next;
        trie() : isFile(false) {}
    };
    class FileSystem {
    public:
        FileSystem() {
            root = new trie();
        }
        vector<string> ls(string &path) {
            vector<string> pathArray = pathSplit(path);
            trie* cur = root;
            for (const auto &each : pathArray) {
                cur = cur->next[each];
            }
            vector<string> result;
            if (cur->isFile) {
                result.push_back(pathArray.back());
            } else {
                for (const auto &each : cur->next) {
                    result.push_back(each.first);
                }
            }
            return result;
        }
        void mkdir(string &path) {
            vector<string> pathArray = pathSplit(path);
            trie *cur = root;
            for (const auto &each : pathArray) {
                if (cur->next.count(each) == 0) {
                    cur->next[each] = new trie();
                }
                cur = cur->next[each];
            }
        }
        void addContentToFile(string &filePath, const string& content) {
            vector<string> fileArray = pathSplit(filePath);
            trie *cur = root;
            for (const auto &each : fileArray) {
                if (cur->next.count(each) == 0) {
                    cur->next[each] = new trie();
                }
                cur = cur->next[each];
            }
            cur->isFile = true;
            cur->content += content;
        }
        string readContentFromFile(const string& filePath) {
            vector<string> fileArray = pathSplit(filePath);
            trie* cur = root;
            for (const auto &each : fileArray) {
                cur = cur->next[each];
            }
            return cur->content;
        }
    private:
        trie *root;
    
        static vector<string> pathSplit(const string& path)
        {
            stringstream ss(path);
            string s;
            vector<string> answer;
            if (path.empty()) {
                return answer;
            }
            while (getline(ss, s, '/')) {
                answer.push_back(s);
            } //这里注意开始会有一个空字符
            return vector<string>(answer.begin() + 1, answer.end());
        }
    };
    

    相关文章

      网友评论

          本文标题:lt.588 设计内存文件系统

          本文链接:https://www.haomeiwen.com/subject/tjvdiltx.html