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

Note:
- You can assume all file or directory paths are absolute paths which begin with
/and do not end with/except that the path is just"/". - 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.
- You can assume that all directory names and file names only contain lower-case letters, and same names won't exist in the same directory.
分析:https://www.cnblogs.com/grandyang/p/6944331.html
这道题比较tricky的地方是ls这个命令,题目中的例子其实不能很好的展示出ls的要求,其对文件和文件夹的处理方式是不同的。由于这里面的文件没有后缀,所以最后一个字符串有可能是文件,也有可能是文件夹。比如a/b/c,那么最后的c有可能是文件夹,也有可能好是文件,如果c是文件夹的话,ls命令要输出文件夹c中的所有文件和文件夹,而当c是文件的话,只需要输出文件c即可。另外需要注意的是在创建文件夹的时候,路径上没有的文件夹都要创建出来,还有就是在给文件添加内容时,路径中没有的文件夹都要创建出来。
class File {
boolean isFile = false;
Map<String, File> children = new HashMap<>();
String content = "";
}
public class FileSystem {
File root = null;
public FileSystem() {
root = new File();
}
public List<String> ls(String path) {
String[] dirs = path.split("/");
File node = root;
List<String> result = new ArrayList<>();
String name = "";
for (String dir : dirs) {
if (dir.length() == )
continue;
if (!node.children.containsKey(dir)) {
return result;
}
node = node.children.get(dir);
name = dir;
}
if (node.isFile) {
result.add(name);
} else {
for (String key : node.children.keySet()) {
result.add(key);
}
}
Collections.sort(result);
return result;
}
public void mkdir(String path) {
String[] dirs = path.split("/");
File node = root;
for (String dir : dirs) {
if (dir.length() == ) continue;
if (!node.children.containsKey(dir)) {
File file = new File();
node.children.put(dir, file);
}
node = node.children.get(dir);
}
}
public void addContentToFile(String filePath, String content) {
String[] dirs = filePath.split("/");
File node = root;
for (String dir : dirs) {
if (dir.length() == ) continue;
if (!node.children.containsKey(dir)) {
File file = new File();
node.children.put(dir, file);
}
node = node.children.get(dir);
}
node.isFile = true;
node.content += content;
}
public String readContentFromFile(String filePath) {
String[] dirs = filePath.split("/");
File node = root;
for (String dir : dirs) {
if (dir.length() == )
continue;
if (!node.children.containsKey(dir)) {
File file = new File();
node.children.put(dir, file);
}
node = node.children.get(dir);
}
return node.content;
}
}
Design In-Memory File System的更多相关文章
- [LeetCode] Design In-Memory File System 设计内存文件系统
Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...
- [CareerCup] 8.9 An In-memory File System 内存文件系统
8.9 Explain the data structures and algorithms that you would use to design an in-memory file system ...
- File System Design Case Studies
SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Int ...
- Design and Implementation of the Sun Network File System
Introduction The network file system(NFS) is a client/service application that provides shared file ...
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- HDFS分布式文件系统(The Hadoop Distributed File System)
The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...
- Low-overhead enhancement of reliability of journaled file system using solid state storage and de-duplication
A mechanism is provided in a data processing system for reliable asynchronous solid-state device bas ...
- HDFS relaxes a few POSIX requirements to enable streaming access to file system data
https://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction [ ...
- PatentTips – EMC Virtual File System
BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...
随机推荐
- 017_STM32程序移植之_AS608指纹模块
STM32程序移植之AS608指纹模块 BUG说明: 硬件接线图如图所示 STM32引脚 指纹模块引脚 功能 3.3V 3.3V PA3 Tx PA2 Rx GND GND PA1 WAK 3.3V ...
- 009_STM32程序移植之_内部falsh
flash 模拟 EEPROM 实验 1. 测试环境:STM32C8T6 2. 测试接口: 3. 串口使用串口一,波特率9600 单片机引脚------------CH340引脚 VCC---- ...
- Oracle 物理结构(三) 文件-参数文件
一.参数文件介绍 Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的, 决定了数据库的物理 结构.内存.数据库的限制及系统大量的默认值.数据库的 ...
- STS工具引入jar报问题?(问题待解决)
pom.xml文件中先引入ojdbc6,后修改成ojdbc7了,为啥还报下面的错误: Project 'nx-test-mybatis-oracle' is missing required libr ...
- 修复grub rescue问题
前几天,手欠点了下win10的系统升级,直接从17.09升级到了19.3虽然也有些波折,总体顺利,以为一切都完事大吉之时,重启系统,原来,万恶的win10给我挖好了坑,早等着我呢.我去,千万只cnm脑 ...
- instr动态模糊查询
String sqlSearchtext = ""; if(!"".equals(model.getXzqhdm())&&model.getXz ...
- ICEM—三分之一风扇
原视频下载地址:https://yunpan.cn/cSwYBI6sb9vHS 访问密码 9059
- Apache Flink - 分布式运行环境
1.任务和操作链 下面的数据流图有5个子任务执行,因此有五个并行线程. 2.Job Managers, Task Managers, Clients Job Managers:协调分布式运行,他们安排 ...
- MapReduce Combiner
Combiner编程(可选步骤,视情况而定!) combiner最基本是实现本地key的归并,combiner具有类似本地的reduce功能. 如果不用combiner,那么所有的结果都是reduce ...
- C# Under the Hood: async/await (Marko Papic)
https://www.markopapic.com/csharp-under-the-hood-async-await/ Async and await keywords came with C# ...