代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
给你一个目录信息列表 paths
,包括目录路径,以及该目录中的所有文件及其内容,请你按路径返回文件系统中的所有重复文件。答案可按 任意顺序 返回。
一组重复的文件至少包括 两个 具有完全相同内容的文件。
输入 列表中的单个目录信息字符串的格式如下:
"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
这意味着,在目录 root/d1/d2/.../dm
下,有 n
个文件 ( f1.txt
, f2.txt
... fn.txt
) 的内容分别是 ( f1_content
, f2_content
... fn_content
) 。注意:n >= 1
且 m >= 0
。如果 m = 0
,则表示该目录是根目录。
输出 是由 重复文件路径组 构成的列表。其中每个组由所有具有相同内容文件的文件路径组成。文件路径是具有下列格式的字符串:
"directory_path/file_name.txt"
示例 1:
输入:paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"] 输出:[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
示例 2:
输入:paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"] 输出:[["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]]
提示:
1 <= paths.length <= 2 * 104
1 <= paths[i].length <= 3000
1 <= sum(paths[i].length) <= 5 * 105
paths[i]
由英文字母、数字、字符 '/'
、'.'
、'('
、')'
和 ' '
组成
进阶:
方法一:哈希表
我们创建哈希表 d
,其中键是文件内容,值是具有相同内容的文件路径列表。
遍历 paths
,我们处理出每个文件的路径和内容,然后将其添加到哈希表 d
中。
最后,我们返回哈希表 d
中所有具有多个文件路径的值。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 paths
的长度。
class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
d = defaultdict(list)
for p in paths:
ps = p.split()
for f in ps[1:]:
i = f.find('(')
name, content = f[:i], f[i + 1: -1]
d[content].append(ps[0] + '/' + name)
return [v for v in d.values() if len(v) > 1]
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, List<String>> d = new HashMap<>();
for (String p : paths) {
String[] ps = p.split(" ");
for (int i = 1; i < ps.length; ++i) {
int j = ps[i].indexOf('(');
String content = ps[i].substring(j + 1, ps[i].length() - 1);
String name = ps[0] + '/' + ps[i].substring(0, j);
d.computeIfAbsent(content, k -> new ArrayList<>()).add(name);
}
}
List<List<String>> ans = new ArrayList<>();
for (var e : d.values()) {
if (e.size() > 1) {
ans.add(e);
}
}
return ans;
}
}
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
unordered_map<string, vector<string>> d;
for (auto& p : paths) {
auto ps = split(p, ' ');
for (int i = 1; i < ps.size(); ++i) {
int j = ps[i].find('(');
auto content = ps[i].substr(j + 1, ps[i].size() - j - 2);
auto name = ps[0] + '/' + ps[i].substr(0, j);
d[content].push_back(name);
}
}
vector<vector<string>> ans;
for (auto& [_, e] : d) {
if (e.size() > 1) {
ans.push_back(e);
}
}
return ans;
}
vector<string> split(string& s, char c) {
vector<string> res;
stringstream ss(s);
string t;
while (getline(ss, t, c)) {
res.push_back(t);
}
return res;
}
};
func findDuplicate(paths []string) [][]string {
d := map[string][]string{}
for _, p := range paths {
ps := strings.Split(p, " ")
for i := 1; i < len(ps); i++ {
j := strings.IndexByte(ps[i], '(')
content := ps[i][j+1 : len(ps[i])-1]
name := ps[0] + "/" + ps[i][:j]
d[content] = append(d[content], name)
}
}
ans := [][]string{}
for _, e := range d {
if len(e) > 1 {
ans = append(ans, e)
}
}
return ans
}
function findDuplicate(paths: string[]): string[][] {
const d = new Map<string, string[]>();
for (const p of paths) {
const [root, ...fs] = p.split(' ');
for (const f of fs) {
const [name, content] = f.split(/\(|\)/g).filter(Boolean);
const t = d.get(content) ?? [];
t.push(root + '/' + name);
d.set(content, t);
}
}
return [...d.values()].filter(e => e.length > 1);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。