8000 609.Find Duplicate File in System · slee-code/LeetCode@5068416 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 5068416

Browse files
committed
609.Find Duplicate File in System
1 parent e2c02cb commit 5068416

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 题目
2+
3+
找到系统中重复的文件。给定一个目录信息列表,包含目录路径以及文件的内容。找出这个重复的文件的路径。
4+
5+
**举例1:**
6+
7+
```
8+
输入:
9+
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
10+
输出:
11+
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
12+
```
13+
14+
**举例2:**
15+
16+
```
17+
输入:
18+
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(higk)", "root/c/d 4.txt(lmn)", "root 4.txt(opq)"]
19+
输出:
20+
[]
21+
```
22+
23+
# 思路
24+
25+
就是输出重复内容的文件的路径,内容相同的文件放在一个列表里,最终输出的结果是列表嵌套列表的结构。如果没有内容重复 的文件,则输出空列表。
26+
27+
判断内容是否重复,可以使用一个技巧,用集合set()去重,如果去重的数量没变,说明没有重复的,直接输出空列表即可。
28+
29+
# 代码
30+
31+
``` python
32+
class Solution(object):
33+
def findDuplicate(self, paths):
34+
"""
35+
:type paths: List[str]
36+
:rtype: List[List[str]]
37+
"""
38+
all_paths = []
39+
for path in paths:
40+
files = []
41+
tmp = path.split(' ')
42+
nums = len(tmp) - 1
43+
for file in tmp[1:]:
44+
all_paths.append(tmp[0] + '/' + file)
45+
contents = []
46+
for each in all_paths:
47+
contents.append(each[each.index('(')+1:-1])
48+
result = []
49+
if len(set(contents)) == len(contents):
50+
return []
51+
for content in set(contents):
52+
if contents.count(content) > 1:
53+
tmp = []
54+
for i in range(len(contents)):
55+
if content == contents[i]:
56+
tmp.append(all_paths[i][:all_paths[i].index('(')])
57+
result.append(tmp)
58+
return result
59+
```

0 commit comments

Comments
 (0)
0