Design a data structure that simulates an in-memory file system.
Implement the FileSystem class:
FileSystem()Initializes the object of the systemList<String> ls(String path)- Returns the list of directory names in this directory. The answer should be in lexicographic order.
void mkdir(String path)- Makes a new directory according to the given path. If the middle directories in the path do not exist, you should create them as well.
Example:FileSystem fileSystem = new FileSystem();
fileSystem.ls("/"); // return []
fileSystem.mkdir("/a/b/c");
fileSystem.ls("/"); // return ["a"]
这道题重点考察数据结构设计能力,而不是算法复杂度。文件系统本质是一个树结构,每一层路径都是一个节点。mkdir 需要按路径逐层创建节点,缺失的中间目录也要自动补齐;ls 则需要返回当前节点下的子目录,并按字典序排序。难点在于路径解析和结构组织是否清晰,而不是计算本身。
正文完