zoukankan      html  css  js  c++  java
  • [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);
     */
  • 相关阅读:
    10106 Product
    枚举值不占对象空间
    编译器会强制没有数据成员的对象长度非零
    对象切片与虚函数机制
    私有继承成员的公有化
    2013年开发者需要了解的开发趋势和技能
    redis 入门笔记
    Tomcat的异步通讯配置使用NIO
    如何成为“10倍效率的开发者”
    如何安装Node.js
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12695483.html
Copyright © 2011-2022 走看看