@@ -43,64 +43,68 @@ Your output (file and directory names together) should in lexicographic order.
43
43
44
44
*/
45
45
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
+ }
59
63
}
60
- }
61
64
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 );
72
77
}
73
- node = node . map . get ( eachPath ) ;
78
+ return node ;
74
79
}
75
- return node ;
76
- }
77
80
78
- public FileSystem () {
79
- }
81
+ public FileSystem () {
82
+ }
80
83
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
+ }
89
93
}
90
- }
91
94
92
- public void mkdir (String path ) {
93
- dfs (path );
94
- }
95
+ public void mkdir (String path ) {
96
+ dfs (path );
97
+ }
95
98
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
+ }
101
104
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
+ }
104
108
}
105
109
}
106
110
0 commit comments