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







  • 相关阅读:
    快讯:优酷四季度净盈余570万美元
    陈诉称谷歌Apps受大企业欢迎 或对微软构成要挟
    Bus.fm:有颜色的“巴士电台”
    摩根大通预计全球PC市场疲软 中国为主因
    中国联通首批沃Phone终端将于3月上市销售
    酷派将推800元Android智好手机推行3G终端
    谷歌预计将来几年在线告白局限打破1000亿美元
    Facebook与日本电通协作展开告白营销业务
    动静称苹果2日将推出小企业手艺支撑办事
    C# 调用C++生成的dll
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7652661.html
Copyright © 2020-2023  润新知