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 ...
随机推荐
- 解决eslint与webstorm关于script标签的缩进问题
解决eslint与webstorm关于script标签的缩进问题 2018年12月29日 23:16:29 tozeroblog 阅读数 752 问题重现在vue-cli中,使用eslint时会对 ...
- AE开发之默认滚轮缩放功能反置(C#修改注册表数据)
ArcMap默认的滚轮缩放是向下放大,向上缩小 如果想修改成向上放大,向下缩小,直接在ArcMap-Customize-ArcMap Options里, 将最下面的Mouse Wheel and Co ...
- sublime的一些记录
{ "keys": ["tab"], "command": "reindent", "context" ...
- HDU 4393 Throw nails(贪心加模拟,追及问题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=115361#problem/D 题意大致是:给出最多50000个人,拥有最初速度 ...
- 阿里前端实习生面试总结(两轮技术面+一轮hr面)
投的蚂蚁金服: 一面(只有13分钟): 1.angular里双向绑定的实现原理: 巴拉巴拉巴拉,这个问题很常见,我提到了$scope.$apply()和$scope.$digest(),面试官问app ...
- golang gorm框架的默认时区问题
gorm框架的时区是在连接数据库时设置的, 如下
- the requested PHP extension dom is missing from your system
composer 出错 the requested PHP extension dom is missing from your system 解决办法 yum install php70w ...
- mysql 误删除所有用户或者忘记root密码
/etc/init.d/mysqld stop //停止数据库/etc/init.d/mysqld restart //启动数据库(1)开启特殊启动模式mysqld_safe --skip-grant ...
- maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
对Pom文件进行配置(亲自尝试,已成功解决) <build> <plugins> <plugin> <groupId>org.apache.maven. ...
- 如何用CSS3来实现卡片的翻转特效
CSS3实现翻转(Flip)效果 动画效果 效果分析 当鼠标滑过包含块时,元素整体翻转180度,以实现“正”“反”面的切换. HTML分析 分析:.container,.flip为了实现动画效果做准备 ...