File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -757,6 +757,53 @@ class Solution:
757
757
return ans
758
758
```
759
759
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
+
760
807
## 652. 寻找重复的子树
761
808
762
809
[ 原题链接] ( https://leetcode-cn.com/problems/find-duplicate-subtrees/ )
You can’t perform that action at this time.
0 commit comments