Skip to content

Commit 8cf9fd3

Browse files
refactor 609
1 parent 6719bd9 commit 8cf9fd3

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,34 @@ If the file content is very large (GB level), how will you modify your solution?
5252
*/
5353
public class _609 {
5454

55-
public List<List<String>> findDuplicate(String[] paths) {
56-
Map<String, List<String>> map = new HashMap<>();//key is the file content, value is the list of directories that has this directory/file
57-
for (String path : paths) {
58-
String[] dirAndFiles = path.split(" ");
59-
for (int i = 1; i < dirAndFiles.length; i++) {
60-
String content = dirAndFiles[i].substring(dirAndFiles[i].indexOf("(") + 1, dirAndFiles[i].indexOf(")"));
61-
if (!map.containsKey(content)) {
62-
map.put(content, new ArrayList<>());
55+
public static class Solution1 {
56+
public List<List<String>> findDuplicate(String[] paths) {
57+
Map<String, List<String>> map = new HashMap<>();//key is the file content, value is the list of directories that has this directory/file
58+
for (String path : paths) {
59+
String[] dirAndFiles = path.split(" ");
60+
for (int i = 1; i < dirAndFiles.length; i++) {
61+
String content = dirAndFiles[i].substring(dirAndFiles[i].indexOf("(") + 1, dirAndFiles[i].indexOf(")"));
62+
if (!map.containsKey(content)) {
63+
map.put(content, new ArrayList<>());
64+
}
65+
List<String> dirs = map.get(content);
66+
dirs.add(dirAndFiles[0] + "/" + dirAndFiles[i].substring(0, dirAndFiles[i].indexOf("(")));
67+
map.put(content, dirs);
6368
}
64-
List<String> dirs = map.get(content);
65-
dirs.add(dirAndFiles[0] + "/" + dirAndFiles[i].substring(0, dirAndFiles[i].indexOf("(")));
66-
map.put(content, dirs);
6769
}
68-
}
6970

70-
List<List<String>> result = new ArrayList<>();
71-
for (String content : map.keySet()) {
72-
if (map.get(content).size() > 1) {
73-
List<String> dupFile = new ArrayList<>();
74-
for (String dir : map.get(content)) {
75-
dupFile.add(dir);
71+
List<List<String>> result = new ArrayList<>();
72+
for (String content : map.keySet()) {
73+
if (map.get(content).size() > 1) {
74+
List<String> dupFile = new ArrayList<>();
75+
for (String dir : map.get(content)) {
76+
dupFile.add(dir);
77+
}
78+
result.add(dupFile);
7679
}
77-
result.add(dupFile);
7880
}
81+
return result;
7982
}
80-
return result;
8183
}
8284

8385
/**Answers to follow-up questions:

src/test/java/com/fishercoder/_609Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
import java.util.List;
99

1010
public class _609Test {
11-
private static _609 test;
11+
private static _609.Solution1 solution1;
1212
private static String[] paths;
1313
private static List<List<String>> actual;
1414

1515
@BeforeClass
1616
public static void setup() {
17-
test = new _609();
17+
solution1 = new _609.Solution1();
1818
}
1919

2020
@Test
2121
public void test1() {
2222
paths = new String[]{"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"};
23-
actual = test.findDuplicate(paths);
23+
actual = solution1.findDuplicate(paths);
2424
CommonUtils.printListList(actual);
2525
}
2626
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy