← 返回 snowflake 的题目列表设计内存文件系统
类型:online_judge
Design an in-memory file system that supports the following operations:
ls: List the directory content in lexicographical order. If the path is a file, return a list containing only this file's name.
mkdir: Create a new directory.
addContentToFile: Append content to a file.
readContentFromFile: Return the content of a file.
Use the following interface:
FileSystem(): Initialize the file system object.
ls(string path) -> List[string]: List directory and file names.
mkdir(string path) -> None: Create a directory.
addContentToFile(string filePath, string content) -> None: Append content to a file.
readContentFromFile(string filePath) -> string: Return the file content.
Example Test Cases:
Initialize file system
fileSystem = FileSystem()
Call mkdir("/a/b/c")
Call addContentToFile("/a/b/c/d", "hello")
Call ls("/") // Returns ["a"]
Call ls("/a/b/c/d") // Returns ["d"]
Call readContentFromFile("/a/b/c/d") // Returns "hello"
Constraints:
Path length and content will be within 100 characters.
Directory and file names consist of alphanumeric characters only.
No more than 10,000 calls will be made to the system.
Example
Input
fileSystem = FileSystem()
fileSystem.mkdir("/a/b/c")
fileSystem.addContentToFile("/a/b/c/d", "hello")
print(fileSystem.ls("/")) # ["a"]
print(fileSystem.ls("/a/b/c/d")) # ["d"]
print(fileSystem.readContentFromFile("/a/b/c/d")) # "hello"