[Leetcode] 609. Find Duplicate File in System 解题报告

Spark on Yarn 高可用模式部署流程 本文介绍了如何配置Spark和Yarn的高可用(HA)环境,以确保在大规模数据处理中避免单点故障。 阅读详情

题目:

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.

A group of duplicate files consists of at least two files that have exactly the same content.

A single directory info string in the input list has the following format:

"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"

It means there are n files (f1.txt, f2.txt ... fn.txt with content f1_content, f2_content ... fn_content, respectively) in directory root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.

The output is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:

"directory_path/file_name.txt"

Example 1:

Input:
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
Output:  
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]

Note:

  1. No order is required for the final output.
  2. You may assume the directory name, file name and file content only has letters and digits, and the length of file content is in the range of [1,50].
  3. The number of files given is in the range of [1,20000].
  4. You may assume no files or directories share the same name in the same directory.
  5. You may assume each given directory info represents a unique directory. Directory path and file info are separated by a single blank space.

Follow-up beyond contest:
  1. Imagine you are given a real file system, how will you search files? DFS or BFS?
  2. If the file content is very large (GB level), how will you modify your solution?
  3. If you can only read the file by 1kb each time, how will you modify your solution?
  4. What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize?
  5. How to make sure the duplicated files you find are not false positive?

思路:

算法上的难度不大,我觉得主要在于如何很好地回答follow up问题。所以我首先简单地总结一下思路,然后重点探讨如何回答follow up(自己的回答不一定准确,仅供参考)。

算法上关键还是在于建立一个哈希表,建立从fn_content到文件全路径(folder_path + "/" + fn)数组之间的映射。对于paths中的每一个string,我们用istringstream进行解析,其第一个string就是folder_name,之后的每个string就是该文件夹下面的一个fn + fn_content。解析出来之后,再进一步解析fn和fn_content,然后将对应数据加入哈希表中。最后遍历一遍哈希表,找出同样内容出现在多个文件中的项目,加入结果集中即可。

关于follow-up:

1)如果是一个真实的文件系统,那么其实DFS和BFS搜索都是可以的。不同点在于用DFS搜索时,如果文件深度太深,那么可能会需要占用较大的内存栈空间;用BFS时,如果每个文件夹下面的文件夹数量太多,则队列可能会比较长,因此也会占用较大的内存空间。

2)如果文件内容过大,那么显然就不能将文件内容直接作为哈希表的key了。我觉得有几种可能的改进方法:a)用MD5算法(或者其它类似的算法)生成文件内容对应的key,然后用这个key作为哈希表的key;2)采用多次哈希的方法,例如第一次用fn_content的长度来作为key,第二次处理的时候,在长度相同的文件列表中,再用MD5算法判断文件内容是不是相同。这样做的好处在于第一次哈希的时候,就排除了大量内容大小不同的文件,从而使得需要用MD5算法的文件数目大大减少,从而提高效率。

3)那就每读进来1kb的数据,做一次MD5吧,最后把所有的MD5结果生成一个向量,或者再次MD5,用最终结果来作为哈希表的key。

4)如果认为平均每个文件夹下面有常数个文件(夹),那么修改后的算法的时间复杂度就是O(file_counts * avg_file_length),其中file_counts表示文件系统中所有的文件个数;avg_file_length表示平均文件长度。最耗时的有可能是两部分:a)遍历文件的部分(如果文件数量特别大);b)读取每个文件内容的部分(如果每个文件特别大)。要优化,那么除了采用3)中的方法之外,还可以采用并行化方法来处理。例如遍历文件的时候,采用多线程或者map reduce的思路进行并行,最后再合成所有的结果。读取每个文件内容的时候,也可以采用多线程(需要硬盘支持了,有这样的硬盘吗?)。

5)再牛逼的哈希算法也不能保证完全消除冲突,那么为了make sure,就只好最后对结果采用最笨的方法进行double check了。。。

代码:

class Solution {
public:
    vector<vector<string>> findDuplicate(vector<string>& paths) {
        unordered_map<string, vector<string>> hash;     // {fn_content, fn_path}
        for (int i = 0; i < paths.size(); ++i) {
            string folder_path;
            vector<string> files;
            resolve(paths[i], folder_path, files);
            for (int j = 0; j < files.size(); ++j) {
                string name, content;
                getNameContent(files[j], name, content);
                string path = folder_path + "/" + name;
                hash[content].push_back(path);
            }
        }
        vector<vector<string>> ret;
        for (auto it = hash.begin(); it != hash.end(); ++it) {
            if (it->second.size() > 1) {
                ret.push_back(it->second);
            }
        }
        return ret;
    }
private:
    void resolve(const string &s, string &folder_path, vector<string> &files) {
        istringstream iss(s);
        iss >> folder_path;
        string file;
        while (!iss.eof()) {
            iss >> file;
            files.push_back(file);
        }
    }
    void getNameContent(const string &file, string &name, string &content) {
        int index = file.find('(');
        name = file.substr(0, index);
        content = file.substr(index);
    }
};
SOME/IP协议详解「1.0·概述」 主要讲解什么是someip, 阅读详情

相关推荐

用FSL的eddy_topup pipeline完整处理DTI数据:从原理到实战,搞定涡流和磁化率畸变

本文详细介绍了如何使用FSL的eddy_topup pipeline完整处理DTI数据,解决涡流和磁化率畸变问题。从原理到实战,涵盖了数据格式转换、脑提取、b0图像处理等关键步骤,并提供了acqparams.txt配置和eddy命令的详细解析,帮助研究人员高效完成DTI数据校正。

weixin_29235525的博客 281

leetcode | go | 第609题 | 在系统中查找重复文件

【代码】leetcode | go | 第609题 | 在系统中查找重复文件。

iCling的博客 651

达梦DM8整合Seata AT模式实战:手把手教你搞定分布式事务(含源码修改避坑)

本文详细介绍了达梦DM8数据库与Seata AT模式的深度整合实战,包括环境准备、架构设计、源码适配及分布式事务落地。通过独创性源码改造,解决了Seata官方对达梦数据库AT模式支持的限制,为国产数据库在微服务架构中的应用提供了可靠的事务保障。

weixin_30764771的博客 332

leetcode 609. Find Duplicate File in System 在系统中查找重复文件

摘要: 该解决方案用于在文件系统中查找内容重复的文件。使用哈希表存储文件内容(content)与对应文件路径列表的映射关系。通过遍历每个路径字符串,提取文件路径和内容,并将完整路径存入哈希表。最后筛选出内容重复(对应多个文件路径)的条目作为结果返回。主要涉及字符串查找和分割操作,时间复杂度为O(n*m),其中n是路径数,m是平均路径长度。

邹九的个人博客 235

ROS专栏—基于STM32F103的INA219电源数据采集

文章主要记录了stm32f103对ina219的数据采集

热爱生活、奋斗前行 2351

51单片机-点亮LED和蜂鸣器

ULN2003是高耐压、大电流达林顿陈列,由七个硅NPN达林顿管组成。ULN2003是大电流驱动陈列,多用于单片机、只能仪表、PLC、数字量输出卡等控制电路中。可直接驱动蜂鸣器、继电器等负载。void delay(u16 i) // i等于1大约10uswhile(i--);//取反,高低电平不断变化,产生脉冲delay(10);// 周期200us。

三日沐水 1125

Leetcode——609. Find Duplicate File in System

题目原址 https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目描述 给定一个String类型的数组,数组中包含文件的根目录和文件名称,文件名称后面包含的是文件的内容,现在要求将内容相同的文件目录放在一个List中,将多个包含不同内容的List放入一个List&lt;List&lt;&gt;&gt...

想当厨子的程序媛 465

2026年北京君正数字IC笔试试卷带答案

本文摘要:该文档是一份数字IC设计相关的考试试题,包含单选题、多选题、波形分析题和代码设计题。内容涵盖时序约束、半导体工艺、状态机设计、逻辑综合、异步FIFO设计等知识点。试题解析详细,重点考察数字电路基础、时序分析和Verilog代码实现能力。典型题目包括多周期路径设置、Moore/Mealy状态机区别、同步复位与异步复位特性、RISC-V架构特点等,并提供了异步FIFO的Verilog实现参考代码。(149字)

Open 112

609. Find Duplicate File in System

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their p

gjwei1993的博客 347

YOLOv13改进策略【Neck篇】| GSConv 轻量卷积,Slim-Neck 减参数还不掉点

Neck 篇的第二篇继续"替换式"改进,这次的对象是 Neck 里的下采样卷积。GSConv 出自《Slim-neck by GSConv: a better design of lightweight detector》(2022),它的出发点:DWSConv(深度可分离)轻但"信息混不进通道",标准卷积重但表达好——GSConv 用GConv(分组卷积)混通道 + DWConv 补空间 + Channel Shuffle 打散,两全其美。

qq_53329130的博客 30

nvidia系列教程-AGX-Orin系统刷机及备份

NVIDIA AGX Orin 是一款高性能的嵌入式计算平台,专为边缘计算和 AI 应用而设计。为了确保系统的稳定性和适应不同的应用场景,用户可能需要对 AGX Orin 进行系统刷机和备份操作。本文将详细介绍如何完成这些操作,帮助您更好地管理和维护 AGX Orin 设备。通过以上步骤,您可以成功地为 NVIDIA AGX Orin 刷写系统并创建备份。这些操作不仅能帮助您保持系统的更新和安全,还能在遇到问题时快速恢复系统。

flypig has a dream! 3323

LeetCode-609. Find Duplicate File in System

Description Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in ...

zy2317878的博客 336

Python爬虫实战:新浪财经股票金融数据全方位抓取教程

本教程系统介绍了新浪财经股票数据爬取技术,从静态网页解析、实时接口请求到异步爬取和数据存储,帮助你搭建了一个完整的金融数据采集流程。未来,可以结合深度学习模型进行行情预测,或者将数据接入量化交易系统,实现自动化投资。

2201_76125261的博客 2209

LeetCode 609. Find Duplicate File in System解题报告(python)

609. Find Duplicate File in System Find Duplicate File in System python solution 题目描述 Given a list of directory info including directory path, and all the files with contents in this directory, you ...

Orientliu96的博客 329

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【LeetCode】609. Find Duplicate File in System 解题报告(Python & C++)

【LeetCode】609. Find Duplicate File in System 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目描述: Given a list of directory info ...

负雪明烛 1277

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode //C - 609. Find Duplicate File in System

【代码】LeetCode //C - 609. Find Duplicate File in System。

Made in Code 1120

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

609. Find Duplicate File in System**

609. Find Duplicate File in System** https://leetcode.com/problems/find-duplicate-file-in-system/ 题目描述 Given a list of directory info including directory path, and all the files with contents in this ...

珍妮的选择的博客 379

Leetcode [609. Find Duplicate File in System]

Problem:609. Find Duplicate File in SystemQuestionGiven a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of dupl

a1042651820的博客 300

[leetcode] 609. Find Duplicate File in System

Description Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in ter...

Learning from the mistakes 332

leetcode 609. Find Duplicate File in System

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of thei

淡然坊 435

【leetcode】609. Find Duplicate File in System

题目如下: Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms ...

weixin_34067980的博客 123

LeetCode 609: Find Duplicate File in System(python)

查找并输出内容相同的文件的目录

marlonlyh的博客 715

LeetCode #609 - Find Duplicate File in System

题目描述: Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of...

LawFile的博客 229

【Leetcode】609. Find Duplicate File in System

题目地址: https://leetcode.com/problems/find-duplicate-file-in-system/ 给定nnn个字符串,这些字符串的格式是,一个目录 + 空格 + 文件名(内容) + 空格 + 文件名(内容)……。求内容重复的文件路径,以列表嵌套列表的形式返回,每个内列表就是内容相同的文件路径。如果有文件的内容是独一无二的,则不统计它。 开个哈希表,直接对每个字符串,解析出内容和文件路径,然后将内容相同的文件路径归纳起来即可。代码如下: import java.util.A

数学、算法爱好者的博客 144

leetcode 609. Find Duplicate File in System(找到相同的文件)

leetcode 609

蓝羽飞鸟的博客 433

[leetcode]609. Find Duplicate File in System

题目链接:https://leetcode.com/problems/find-duplicate-file-in-system/#/description Given a list of directory info including directory path, and all the files with contents in this directory, you need

xiaocong1990的博客 388
上一篇: [Leetcode] 606. Construct String from Binary Tree 解题报告
下一篇: [Leetcode] 611. Valid Triangle Number 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值