• [LC] 588. Design In-Memory File System


    Design an in-memory file system to simulate the following functions:

    ls: Given a path in string format. If it is a file path, return a list that only contains this file's name. If it is a directory path, return the list of file and directory names in this directory. Your output (file and directory names together) should in lexicographic order.

    mkdir: Given a directory path that does not exist, you should make a new directory according to the path. If the middle directories in the path don't exist either, you should create them as well. This function has void return type.

    addContentToFile: Given a file path and file content in string format. If the file doesn't exist, you need to create that file containing given content. If the file already exists, you need to append given content to original content. This function has void return type.

    readContentFromFile: Given a file path, return its content in string format.

    Example:

    Input: 
    ["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"]
    [[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]]
    
    Output:
    [null,[],null,null,["a"],"hello"]
    
    class FileSystem {
    
        private Node root;
        public FileSystem() {
            root = new Node("/");
        }
        
        public List<String> ls(String path) {
            Node cur = traversal(path);
            List<String> list = new ArrayList<>();
            if (cur.hasFile) {
                // add file name
                list.add(cur.name);
            } else {
                for (String child: cur.children.keySet()) {
                    list.add(child);
                }
            }
            Collections.sort(list);
            return list;
        }
        
        public void mkdir(String path) {
            traversal(path);
        }
        
        public void addContentToFile(String filePath, String content) {
            Node cur = traversal(filePath);
            cur.content.append(content);
            cur.hasFile = true;
        }
        
        public String readContentFromFile(String filePath) {
            Node cur = traversal(filePath);
            return cur.content.toString();
        }
        
        private Node traversal(String path) {
            String[] pathArr = path.split("/");
            Node cur = root;
            for (int i = 1; i < pathArr.length; i++) {
                if (!cur.children.containsKey(pathArr[i])) {
                    Node node = new Node(pathArr[i]);
                    cur.children.put(pathArr[i], node);
                }
                cur = cur.children.get(pathArr[i]);
            }
            return cur;
        } 
    }
    
    class Node {
        String name;
        StringBuilder content;
        Map<String, Node> children;
        boolean hasFile;
        public Node(String name) {
            this.name = name;
            content = new StringBuilder();
            children = new HashMap<>();
        }
    }
    
    /**
     * Your FileSystem object will be instantiated and called as such:
     * FileSystem obj = new FileSystem();
     * List<String> param_1 = obj.ls(path);
     * obj.mkdir(path);
     * obj.addContentToFile(filePath,content);
     * String param_4 = obj.readContentFromFile(filePath);
     */
  • 相关阅读:
    jQuery源代码学习笔记_01
    jQuery学习心得
    前端笔试题目练习
    JavaScript Date学习实例:获取3分钟前的时间“hhmmss”格式
    JavaScript Date 学习心得
    Javascript 中的false、0、null、undefined和空字符串对象
    JavaScript比较运算符——"== != === !=="区别
    JavaScript运算符优先级——"++,--,&&,||“
    JavaScript操作符(=?,)优先级
    JavaScript控制流及关键字与C语言之比较
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12695483.html
Copyright © 2020-2023  润新知