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 ...
随机推荐
- mysql常用的索引种类
一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度 二.索引类型 Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RT ...
- SessionState,默認mode應該是"InProc"
在ASP.NET的sessionState的三種屬性 http://www.dotblogs.com.tw/boei/archive/2010/07/06/16414.aspx需要在另外的config ...
- [Luogu] 最大收益
题面:https://www.luogu.org/problemnew/show/P2647 题解:https://www.zybuluo.com/wsndy-xx/note/1142685
- linux中如何修改最大文件句柄数
1.使用ulimit -a可以查看,其中的open files后面的数就是最大文件句柄数 2.临时方法:使用ulimit -n size修改最大文件句柄数(这种方法只针对当前进程有效) 3.永久方法: ...
- ACM之路(12)—— KMP & 扩展KMP & Manacher
最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...
- slax linux的定制
由于数据结构教学的需要,需要用到linux,要求就是小,启动快,可定制性强,恰好slax正好满足要求,以下就是定制slax linux的过程记录: 什么是Slax Slax是一个基于Linux的Liv ...
- Raspberry Pi 摄像头模块入门
目录 一.摄像头模块安装 二.使用命令控制摄像头 三.使用Python程序控制摄像头 四.基于vlc的Raspberry Pi摄像头实时监控 参考资料 Raspberry Pi提供了摄像头模块的接口, ...
- tp5 回滚事务记录,其中一条语句报错,全部回滚
#################################### 测试事务 // 启动事务 Db::startTrans(); try { //插入行为表 $data = [ 'userId' ...
- Spring Security整合JWT,实现单点登录,So Easy~!
前面整理过一篇 SpringBoot Security前后端分离,登录退出等返回json数据,也就是用Spring Security,基于SpringBoot2.1.4 RELEASE前后端分离的情况 ...
- Change Assembly Version in a compiled .NET assembly
Change Assembly Version in a compiled .NET assembly You can use ILMerge: ILMerge.exe Foo.dll /ver:1. ...