Skip to content

Commit 8b55ef9

Browse files
committedMay 21, 2021
🐱(hash): 609. 在系统中查找重复文件
1 parent 01be755 commit 8b55ef9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
 

‎docs/data-structure/hash/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,53 @@ class Solution:
757757
return ans
758758
```
759759

760+
## 609. 在系统中查找重复文件
761+
762+
[原题链接](https://leetcode-cn.com/problems/find-duplicate-file-in-system/)
763+
764+
### 思路
765+
766+
使用哈希表实现。将文件内容作为哈希 key 值,文件路径数组作为 value。
767+
768+
使用 Go 语言的哈希 + 数组表示:`contentMap := make(map[string][]string)`
769+
770+
### 实现
771+
772+
```go
773+
import "strings"
774+
775+
func findDuplicate(paths []string) [][]string {
776+
contentMap := make(map[string][]string)
777+
ans := make([][]string, 0)
778+
for _, pathString := range paths {
779+
// 路径分割
780+
paths := strings.Split(pathString, " ")
781+
document := ""
782+
for idx, path := range paths {
783+
if idx == 0 {
784+
document = path
785+
} else {
786+
// 按括号切分
787+
contents := strings.Split(path, "(")
788+
filePath := contents[0]
789+
content := contents[1][:len(contents[1]) - 1]
790+
791+
fullPath := document + "/" + filePath
792+
contentMap[content] = append(contentMap[content], fullPath)
793+
}
794+
}
795+
}
796+
797+
for _, contents := range contentMap {
798+
if len(contents) >= 2 {
799+
ans = append(ans, contents)
800+
}
801+
}
802+
803+
return ans
804+
}
805+
```
806+
760807
## 652. 寻找重复的子树
761808

762809
[原题链接](https://leetcode-cn.com/problems/find-duplicate-subtrees/)

0 commit comments

Comments
 (0)
Please sign in to comment.