|
69 | 69 |
|
70 | 70 | <!-- 这里可写通用的实现逻辑 -->
|
71 | 71 |
|
| 72 | +**方法一:哈希表** |
| 73 | + |
| 74 | +`key` 为文件内容,`value` 为文件路径数组,最后取出所有长度大于 1 的 `value` 即答案。 |
| 75 | + |
72 | 76 | <!-- tabs:start -->
|
73 | 77 |
|
74 | 78 | ### **Python3**
|
75 | 79 |
|
76 | 80 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
77 | 81 |
|
78 | 82 | ```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 |
80 | 99 | ```
|
81 | 100 |
|
82 | 101 | ### **Java**
|
83 | 102 |
|
84 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
85 | 104 |
|
86 | 105 | ```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 | +``` |
87 | 156 |
|
| 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 | +}; |
88 | 194 | ```
|
89 | 195 |
|
90 | 196 | ### **...**
|
|
0 commit comments