Skip to content

Commit 806d87c

Browse files
refactor 588
1 parent 0911604 commit 806d87c

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

src/main/java/com/fishercoder/solutions/_588.java

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,64 +43,68 @@ Your output (file and directory names together) should in lexicographic order.
4343
4444
*/
4545
public class _588 {
46-
/**Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_588.java*/
47-
public static class FileSystem {
48-
49-
class TrieNode {
50-
boolean isFile = false;
51-
Map<String, TrieNode> map;
52-
String name;
53-
StringBuilder stringBuilder;
54-
55-
TrieNode(String name) {
56-
this.name = name;
57-
map = new HashMap<>();
58-
stringBuilder = new StringBuilder();
46+
public static class Solution1 {
47+
/**
48+
* Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_588.java
49+
*/
50+
public static class FileSystem {
51+
52+
class TrieNode {
53+
boolean isFile = false;
54+
Map<String, TrieNode> map;
55+
String name;
56+
StringBuilder stringBuilder;
57+
58+
TrieNode(String name) {
59+
this.name = name;
60+
map = new HashMap<>();
61+
stringBuilder = new StringBuilder();
62+
}
5963
}
60-
}
6164

62-
TrieNode root = new TrieNode("");
63-
64-
TrieNode dfs(String path) {
65-
TrieNode node = root;
66-
for (String eachPath : path.split("/")) {
67-
if (eachPath.isEmpty()) {
68-
continue;
69-
}
70-
if (!node.map.containsKey(eachPath)) {
71-
node.map.put(eachPath, new TrieNode(eachPath));
65+
TrieNode root = new TrieNode("");
66+
67+
TrieNode dfs(String path) {
68+
TrieNode node = root;
69+
for (String eachPath : path.split("/")) {
70+
if (eachPath.isEmpty()) {
71+
continue;
72+
}
73+
if (!node.map.containsKey(eachPath)) {
74+
node.map.put(eachPath, new TrieNode(eachPath));
75+
}
76+
node = node.map.get(eachPath);
7277
}
73-
node = node.map.get(eachPath);
78+
return node;
7479
}
75-
return node;
76-
}
7780

78-
public FileSystem() {
79-
}
81+
public FileSystem() {
82+
}
8083

81-
public List<String> ls(String path) {
82-
TrieNode node = dfs(path);
83-
if (node.isFile) {
84-
return Arrays.asList(node.name);
85-
} else {
86-
List<String> files = new ArrayList(node.map.keySet());
87-
Collections.sort(files);
88-
return files;
84+
public List<String> ls(String path) {
85+
TrieNode node = dfs(path);
86+
if (node.isFile) {
87+
return Arrays.asList(node.name);
88+
} else {
89+
List<String> files = new ArrayList(node.map.keySet());
90+
Collections.sort(files);
91+
return files;
92+
}
8993
}
90-
}
9194

92-
public void mkdir(String path) {
93-
dfs(path);
94-
}
95+
public void mkdir(String path) {
96+
dfs(path);
97+
}
9598

96-
public void addContentToFile(String filePath, String content) {
97-
TrieNode node = dfs(filePath);
98-
node.isFile = true;
99-
node.stringBuilder.append(content);
100-
}
99+
public void addContentToFile(String filePath, String content) {
100+
TrieNode node = dfs(filePath);
101+
node.isFile = true;
102+
node.stringBuilder.append(content);
103+
}
101104

102-
public String readContentFromFile(String filePath) {
103-
return dfs(filePath).stringBuilder.toString();
105+
public String readContentFromFile(String filePath) {
106+
return dfs(filePath).stringBuilder.toString();
107+
}
104108
}
105109
}
106110

src/test/java/com/fishercoder/_588Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
*/
1515
public class _588Test {
1616

17-
private static _588.FileSystem fileSystem;
17+
private static _588.Solution1.FileSystem fileSystem;
1818

1919
@BeforeClass
2020
public static void setup() {
2121
}
2222

2323
@Test
2424
public void test1() {
25-
fileSystem = new _588.FileSystem();
25+
fileSystem = new _588.Solution1.FileSystem();
2626
List<String> list = new ArrayList<>();
2727
assertEquals(list, fileSystem.ls("/"));
2828
fileSystem.mkdir("/a/b/c");
@@ -34,7 +34,7 @@ public void test1() {
3434

3535
@Test
3636
public void test2() {
37-
fileSystem = new _588.FileSystem();
37+
fileSystem = new _588.Solution1.FileSystem();
3838
List<String> list = new ArrayList<>();
3939
assertEquals(list, fileSystem.ls("/"));
4040
fileSystem.mkdir("/a/b/c");

0 commit comments

Comments
 (0)