ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 588. Design In-Memory File System

2022-07-24 07:31:34  阅读:324  来源: 互联网

标签:String filePath System 588 content Design file path public


 

Design a data structure that simulates an in-memory file system.

Implement the FileSystem class:

  • FileSystem() Initializes the object of the system.
  • List<String> ls(String path)
    • If path is a file path, returns a list that only contains this file's name.
    • If path is a directory path, returns the list of file and directory names in this directory.
    The answer should in lexicographic order.
  • void mkdir(String path) Makes a new directory according to the given path. The given directory path does not exist. If the middle directories in the path do not exist, you should create them as well.
  • void addContentToFile(String filePath, String content)
    • If filePath does not exist, creates that file containing given content.
    • If filePath already exists, appends the given content to original content.
  • String readContentFromFile(String filePath) Returns the content in the file at filePath.

Example 1:

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"]

Explanation
FileSystem fileSystem = new FileSystem();
fileSystem.ls("/");                         // return []
fileSystem.mkdir("/a/b/c");
fileSystem.addContentToFile("/a/b/c/d", "hello");
fileSystem.ls("/");                         // return ["a"]
fileSystem.readContentFromFile("/a/b/c/d"); // return "hello"

Constraints:

  • 1 <= path.length, filePath.length <= 100
  • path and filePath are absolute paths which begin with '/' and do not end with '/' except that the path is just "/".
  • You can assume that all directory names and file names only contain lowercase letters, and the same names will not exist in the same directory.
  • You can assume that all operations will be passed valid parameters, and users will not attempt to retrieve file content or list a directory or file that does not exist.
  • 1 <= content.length <= 50
  • At most 300 calls will be made to lsmkdiraddContentToFile, and readContentFromFile.

设计内存文件系统。

Java实现

 1 class FileSystem {
 2     private class FileNode {
 3         private TreeMap<String, FileNode> children;
 4         private StringBuilder file;
 5         private String name;
 6 
 7         public FileNode(String name) {
 8             children = new TreeMap<>();
 9             file = new StringBuilder();
10             this.name = name;
11         }
12 
13         public String getContent() {
14             return file.toString();
15         }
16 
17         public String getName() {
18             return name;
19         }
20 
21         public void addContent(String content) {
22             file.append(content);
23         }
24 
25         public boolean isFile() {
26             return file.length() > 0;
27         }
28 
29         public List<String> getList() {
30             List<String> list = new ArrayList<>();
31             if (isFile()) {
32                 list.add(getName());
33             } else {
34                 list.addAll(children.keySet());
35             }
36             return list;
37         }
38     }
39 
40     private FileNode root;
41     
42     public FileSystem() {
43         root = new FileNode("");
44     }
45     
46     public List<String> ls(String path) {
47         return findNode(path).getList();
48     }
49     
50     public void mkdir(String path) {
51         findNode(path);
52     }
53     
54     public void addContentToFile(String filePath, String content) {
55         findNode(filePath).addContent(content);
56     }
57     
58     public String readContentFromFile(String filePath) {
59         return findNode(filePath).getContent();
60     }
61 
62     private FileNode findNode(String path) {
63         String[] files = path.split("/");
64         FileNode cur = root;
65         for (String file : files) {
66             if (file.length() == 0) {
67                 continue;
68             }
69             cur.children.putIfAbsent(file, new FileNode(file));
70             cur = cur.children.get(file);
71             if (cur.isFile()) {
72                 break;
73             }
74         }
75         return cur;
76     }
77 }
78 
79 /**
80  * Your FileSystem object will be instantiated and called as such:
81  * FileSystem obj = new FileSystem();
82  * List<String> param_1 = obj.ls(path);
83  * obj.mkdir(path);
84  * obj.addContentToFile(filePath,content);
85  * String param_4 = obj.readContentFromFile(filePath);
86  */

 

LeetCode 题目总结

 

标签:String,filePath,System,588,content,Design,file,path,public
来源: https://www.cnblogs.com/cnoodle/p/16513790.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有