zoukankan      html  css  js  c++  java
  • 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 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.txtf2.txt ... fn.txt with content f1_contentf2_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?
    给出目录信息列表(包括目录路径)以及包含此目录中所有内容的所有文件,您需要根据路径找出文件系统中的所有重复文件组。
    1. /**
    2. * @param {string[]} paths
    3. * @return {string[][]}
    4. */
    5. var findDuplicate = function(paths) {
    6. let m = {};
    7. for (let i in paths) {
    8. let data = paths[i].split(" ");
    9. let path = data[0];
    10. for (let index = 1; index < data.length; index++) {
    11. let file = data[index];
    12. let contentIndex = file.indexOf("(");
    13. let content = file.slice(contentIndex + 1, file.length - 1);
    14. let fileName = file.slice(0, contentIndex);
    15. if (m[content]) {
    16. m[content].push(path + "/" + fileName);
    17. } else {
    18. m[content] = [path + "/" + fileName];
    19. }
    20. }
    21. }
    22. let res = [];
    23. for (let i in m) {
    24. if (m[i].length > 1)
    25. res.push(m[i])
    26. }
    27. return res;
    28. };







  • 相关阅读:
    为什么CAP不能同时满足?
    多线程模式下高并发的环境中唯一确保单例模式---DLC双端锁
    有道词典命令行查询工具(Mac/Ubuntu)
    CentOS 6.9配置EPEL源
    GitHub官方Markdown语法教程
    CentOS 6.9设置阿里云源/163源
    Ubuntu 16.04安装Wine版的微信(deepin-wechat)
    普通主板设置BIOS实现电脑插电自动启动
    IntelliJ IDEA导出设置
    Linux下swap分区多大才合适的问题探讨
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7652661.html
Copyright © 2011-2022 走看看