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);
     */
  • 相关阅读:
    PHP的ip2long和long2ip函数的实现原理
    PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简单介绍
    PHP静态延迟绑定简单示例
    [deviceone开发]-do_Socket组件示例
    [deviceone开发]-大家比较关注的应用内部升级
    [deviceone开发]-do_Viewshower的动画效果示例
    [deviceone开发]-企业OA项目开源分享
    [deviceone开发]-do_RichLabel的简单示例
    [deviceone开发]-直播APP心形点赞动画示例
    [deviceone开发]-纳豆项目源码开源
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12695483.html
Copyright © 2011-2022 走看看