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:

  1. 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 "/".
  2. 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.
  3. 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的更多相关文章

  1. [LeetCode] Design In-Memory File System 设计内存文件系统

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  2. [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 ...

  3. File System Design Case Studies

    SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Int ...

  4. Design and Implementation of the Sun Network File System

    Introduction The network file system(NFS) is a client/service application that provides shared file ...

  5. 【LeetCode】1166. Design File System 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...

  6. HDFS分布式文件系统(The Hadoop Distributed File System)

    The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...

  7. 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 ...

  8. 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 [ ...

  9. PatentTips – EMC Virtual File System

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...

随机推荐

  1. Django—Ajax

    Ajax-get url url(r'^ajax_add/', views.ajax_add), url(r'^ajax_demo1/', views.ajax_demo1), 视图 def ajax ...

  2. 畅通工程续(HDU 1874)(简单最短路)

    某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在 ...

  3. docker数据持久化

    转载/参考: https://www.jianshu.com/p/ef0f24fd0674 Docker的数据持久化主要有两种方式: bind mount docker managed volume ...

  4. python爬虫-爬坑之路

    背景简介 爬取外国的某两个网站的数据,网站都没有被墙,爬取三种数据. A: 爬取页面并存储到数据库 B: 爬取页面内的表格内数据并存储到数据库 C: 爬取页面,分析页面并将页面的所有数据分类存入数据库 ...

  5. strom部署问题

    1.storm 引用的 kafka和线上的kafka版本不一致 2.bolt的prepare初始化elasticsearch连接慢,导致第一次处理数据是总是有问题storm调用prepare方法是异步 ...

  6. 哈希表(hash table)基础概念

    哈希是什么 引入:我们在学习数组的时候,使用数组元素的下标值即可访问到该元素,所花费的时间是O(1),与数组元素的个数n没有关系,这就是哈希方法的核心思想. 哈希方法:以关键值K为自变量,通过一定的函 ...

  7. 解决oracle服务占用内存过高的问题

    其实这是因为安装Oracle时,为了均衡电脑性能和数据库性能,默认内存大小为物理内存的1/8,自身内存比较大时,oracle所占的内存也会变大.而通常,我们自己的环境并不需要分配那么大的内存来支持Or ...

  8. Java中路径相关的获取方式

    [参考文章]:Java文件路径(getResource) [参考文章]:关于java:如何获取正在运行的JAR文件的路径? [参考文章]:关于Class.getResource和ClassLoader ...

  9. mysql CONCAT函数

    有时候我们需要使用coacat函数拼接一些字段的生成一个字符串,比如:select concat(field1,field2,field3)  from xxx: 这时候我们就会的到一个这些字段的值拼 ...

  10. HTML、 CSS、 JavaScript三者的关系

    HTML. CSS. JavaScript三者的关系    网页主要由三部分组成: 结构( Structure) . 表现( Presentation) 和行为( Behavior)    HTML ...