Skip to content

Commit 731f2d3

Browse files
committed
feat: add solutions to lc problem: No.0609
No.0609.Find Duplicate File in System
1 parent d97602b commit 731f2d3

File tree

6 files changed

+304
-2
lines changed

6 files changed

+304
-2
lines changed

solution/0600-0699/0609.Find Duplicate File in System/README.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,128 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:哈希表**
73+
74+
`key` 为文件内容,`value` 为文件路径数组,最后取出所有长度大于 1 的 `value` 即答案。
75+
7276
<!-- tabs:start -->
7377

7478
### **Python3**
7579

7680
<!-- 这里可写当前语言的特殊实现逻辑 -->
7781

7882
```python
79-
83+
class Solution:
84+
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
85+
m = defaultdict(list)
86+
for path in paths:
87+
a = path.split(" ")
88+
for i in range(1, len(a)):
89+
j = a[i].find("(")
90+
content = a[i][j + 1 : -1]
91+
name = a[0] + "/" + a[i][:j]
92+
m[content].append(name)
93+
94+
ans = []
95+
for names in m.values():
96+
if len(names) > 1:
97+
ans.append(names)
98+
return ans
8099
```
81100

82101
### **Java**
83102

84103
<!-- 这里可写当前语言的特殊实现逻辑 -->
85104

86105
```java
106+
class Solution {
107+
public List<List<String>> findDuplicate(String[] paths) {
108+
Map<String, List<String>> map = new HashMap<>();
109+
for (String path : paths) {
110+
String[] a = path.split(" ");
111+
for (int i = 1; i < a.length; i++) {
112+
int j = a[i].indexOf('(');
113+
String content = a[i].substring(j + 1, a[i].length() - 1);
114+
String name = a[0] + '/' + a[i].substring(0, j);
115+
List<String> list = map.getOrDefault(content, new ArrayList<>());
116+
list.add(name);
117+
map.put(content, list);
118+
}
119+
}
120+
121+
List<List<String>> ans = new ArrayList<>();
122+
for (List<String> names : map.values()) {
123+
if (names.size() > 1) {
124+
ans.add(names);
125+
}
126+
}
127+
return ans;
128+
}
129+
}
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func findDuplicate(paths []string) [][]string {
136+
m := make(map[string][]string)
137+
for _, path := range paths {
138+
a := strings.Split(path, " ")
139+
for i := 1; i < len(a); i++ {
140+
j := strings.Index(a[i], "(")
141+
content := a[i][j+1 : len(a[i])-1]
142+
name := a[0] + "/" + a[i][:j]
143+
m[content] = append(m[content], name)
144+
}
145+
}
146+
147+
var ans [][]string
148+
for _, names := range m {
149+
if len(names) > 1 {
150+
ans = append(ans, names)
151+
}
152+
}
153+
return ans
154+
}
155+
```
87156

157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
vector<string> split(const string& s, char delim) {
162+
vector<string> result;
163+
stringstream ss(s);
164+
string item;
165+
while (getline(ss, item, delim)) {
166+
result.push_back(item);
167+
}
168+
return result;
169+
}
170+
171+
public:
172+
vector<vector<string>> findDuplicate(vector<string>& paths) {
173+
unordered_map<string, vector<string>> m;
174+
for (auto& path : paths) {
175+
auto a = split(path, ' ');
176+
for (int i = 1; i < a.size(); ++i) {
177+
int j = a[i].find('(');
178+
auto content = a[i].substr(j + 1, a[i].size() - j - 2);
179+
auto name = a[0] + '/' + a[i].substr(0, j);
180+
if (m.find(content) == m.end()) {
181+
m[content] = vector<string>();
182+
}
183+
m[content].emplace_back(name);
184+
}
185+
}
186+
187+
vector<vector<string>> ans;
188+
for (auto& [_, names] : m) {
189+
if (names.size() > 1) ans.emplace_back(names);
190+
}
191+
return ans;
192+
}
193+
};
88194
```
89195

90196
### **...**

solution/0600-0699/0609.Find Duplicate File in System/README_EN.md

+103-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,115 @@
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
65+
m = defaultdict(list)
66+
for path in paths:
67+
a = path.split(" ")
68+
for i in range(1, len(a)):
69+
j = a[i].find("(")
70+
content = a[i][j + 1 : -1]
71+
name = a[0] + "/" + a[i][:j]
72+
m[content].append(name)
73+
74+
ans = []
75+
for names in m.values():
76+
if len(names) > 1:
77+
ans.append(names)
78+
return ans
6479
```
6580

6681
### **Java**
6782

6883
```java
84+
class Solution {
85+
public List<List<String>> findDuplicate(String[] paths) {
86+
Map<String, List<String>> map = new HashMap<>();
87+
for (String path : paths) {
88+
String[] a = path.split(" ");
89+
for (int i = 1; i < a.length; i++) {
90+
int j = a[i].indexOf('(');
91+
String content = a[i].substring(j + 1, a[i].length() - 1);
92+
String name = a[0] + '/' + a[i].substring(0, j);
93+
List<String> list = map.getOrDefault(content, new ArrayList<>());
94+
list.add(name);
95+
map.put(content, list);
96+
}
97+
}
98+
99+
List<List<String>> ans = new ArrayList<>();
100+
for (List<String> names : map.values()) {
101+
if (names.size() > 1) {
102+
ans.add(names);
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func findDuplicate(paths []string) [][]string {
114+
m := make(map[string][]string)
115+
for _, path := range paths {
116+
a := strings.Split(path, " ")
117+
for i := 1; i < len(a); i++ {
118+
j := strings.Index(a[i], "(")
119+
content := a[i][j+1 : len(a[i])-1]
120+
name := a[0] + "/" + a[i][:j]
121+
m[content] = append(m[content], name)
122+
}
123+
}
124+
125+
var ans [][]string
126+
for _, names := range m {
127+
if len(names) > 1 {
128+
ans = append(ans, names)
129+
}
130+
}
131+
return ans
132+
}
133+
```
69134

135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
vector<string> split(const string& s, char delim) {
140+
vector<string> result;
141+
stringstream ss(s);
142+
string item;
143+
while (getline(ss, item, delim)) {
144+
result.push_back(item);
145+
}
146+
return result;
147+
}
148+
149+
public:
150+
vector<vector<string>> findDuplicate(vector<string>& paths) {
151+
unordered_map<string, vector<string>> m;
152+
for (auto& path : paths) {
153+
auto a = split(path, ' ');
154+
for (int i = 1; i < a.size(); ++i) {
155+
int j = a[i].find('(');
156+
auto content = a[i].substr(j + 1, a[i].size() - j - 2);
157+
auto name = a[0] + '/' + a[i].substr(0, j);
158+
if (m.find(content) == m.end()) {
159+
m[content] = vector<string>();
160+
}
161+
m[content].emplace_back(name);
162+
}
163+
}
164+
165+
vector<vector<string>> ans;
166+
for (auto& [_, names] : m) {
167+
if (names.size() > 1) ans.emplace_back(names);
168+
}
169+
return ans;
170+
}
171+
};
70172
```
71173

72174
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
vector<string> split(const string& s, char delim) {
3+
vector<string> result;
4+
stringstream ss(s);
5+
string item;
6+
while (getline(ss, item, delim)) {
7+
result.push_back(item);
8+
}
9+
return result;
10+
}
11+
12+
public:
13+
vector<vector<string>> findDuplicate(vector<string>& paths) {
14+
unordered_map<string, vector<string>> m;
15+
for (auto& path : paths) {
16+
auto a = split(path, ' ');
17+
for (int i = 1; i < a.size(); ++i) {
18+
int j = a[i].find('(');
19+
auto content = a[i].substr(j + 1, a[i].size() - j - 2);
20+
auto name = a[0] + '/' + a[i].substr(0, j);
21+
if (m.find(content) == m.end()) {
22+
m[content] = vector<string>();
23+
}
24+
m[content].emplace_back(name);
25+
}
26+
}
27+
28+
vector<vector<string>> ans;
29+
for (auto& [_, names] : m) {
30+
if (names.size() > 1) ans.emplace_back(names);
31+
}
32+
return ans;
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findDuplicate(paths []string) [][]string {
2+
m := make(map[string][]string)
3+
for _, path := range paths {
4+
a := strings.Split(path, " ")
5+
for i := 1; i < len(a); i++ {
6+
j := strings.Index(a[i], "(")
7+
content := a[i][j+1 : len(a[i])-1]
8+
name := a[0] + "/" + a[i][:j]
9+
m[content] = append(m[content], name)
10+
}
11+
}
12+
13+
var ans [][]string
14+
for _, names := range m {
15+
if len(names) > 1 {
16+
ans = append(ans, names)
17+
}
18+
}
19+
return ans
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<String>> findDuplicate(String[] paths) {
3+
Map<String, List<String>> map = new HashMap<>();
4+
for (String path : paths) {
5+
String[] a = path.split(" ");
6+
for (int i = 1; i < a.length; i++) {
7+
int j = a[i].indexOf('(');
8+
String content = a[i].substring(j + 1, a[i].length() - 1);
9+
String name = a[0] + '/' + a[i].substring(0, j);
10+
List<String> list = map.getOrDefault(content, new ArrayList<>());
11+
list.add(name);
12+
map.put(content, list);
13+
}
14+
}
15+
16+
List<List<String>> ans = new ArrayList<>();
17+
for (List<String> names : map.values()) {
18+
if (names.size() > 1) {
19+
ans.add(names);
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
3+
m = defaultdict(list)
4+
for path in paths:
5+
a = path.split(" ")
6+
for i in range(1, len(a)):
7+
j = a[i].find("(")
8+
content = a[i][j + 1 : -1]
9+
name = a[0] + "/" + a[i][:j]
10+
m[content].append(name)
11+
12+
ans = []
13+
for names in m.values():
14+
if len(names) > 1:
15+
ans.append(names)
16+
return ans

0 commit comments

Comments
 (0)