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. };







  • 相关阅读:
    Ruby笔记四(数组)
    中央直属企业名单【中国级别最高的169家企业】(转)找工作按这个来
    循环pthread_create导致虚拟内存上涨(续1)
    除掉行数小程序
    client comserver编译配置运行详细说明
    网络监听技术概览(转待看)
    查看 linux系统版本,内核,CPU,MEM,位数的相关命令(实验)
    项目中Shell脚本说明(待完善)
    多线程 or 多进程 (实验1)
    循环pthread_create导致虚拟内存上涨(续2)
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7652661.html
Copyright © 2011-2022 走看看