← 返回 perplexity 的题目列表Design In-Memory File System
类型:online_judge
Problem: Design an In-Memory File System
Implement a simplified in-memory file system that supports creating directories/files, appending file content, reading files, and listing directory contents.
The file system starts with the root directory /. All paths are absolute Unix-style paths, for example:
/
/a
/a/b/c
/a/b/c/file.txt
Implement the following operations:
Operations
ls(path)
If path is a file path, return a list containing only the file name.
If path is a directory path, return all direct child file and directory names in lexicographical order.
mkdir(path)
Create the directory path.
If intermediate directories do not exist, create them as well.
If the directory already exists, do nothing.
addContentToFile(filePath, content)
If filePath does not exist, create the file.
If the file already exists, append content to its existing content.
If intermediate directories do not exist, create them as well.
readContentFromFile(filePath)
Return the complete content of the file at filePath.
Input Format
The first line contains an integer Q, the number of operations.
Each of the next Q lines is a JSON array representing one operation:
["ls", path]
["mkdir", path]
["addContentToFile", filePath, content]
["readContentFromFile", filePath]
Only return values of ls and readContentFromFile should be printed.
The result of ls should be printed as a JSON array.
The result of readContentFromFile should be printed as a plain string.
Constraints
1 <= Q <= 10^4
1 <= path.length <= 300
path is a valid absolute Unix-style path starting with /.
File and directory names contain only lowercase letters, digits, dots ., or underscores _.
0 <= content.length <= 10^4
Total file content length is at most 10^6.
readContentFromFile(filePath) is called only when filePath is an existing file.
ls(path) is called only when path exists.
Example
Input
6
["ls", "/"]
["mkdir", "/a/b/c"]
["addContentToFile", "/a/b/c/d", "hello"]
["ls", "/"]
["readContentFromFile", "/a/b/c/d"]
["ls", "/a/b/c/d"]
Output
[]
["a"]
hello
["d"]
Example
Input
6
["ls", "/"]
["mkdir", "/a/b/c"]
["addContentToFile", "/a/b/c/d", "hello"]
["ls", "/"]
["readContentFromFile", "/a/b/c/d"]
["ls", "/a/b/c/d"]
Output
[]
["a"]
hello
["d"]