zoukankan      html  css  js  c++  java
  • LeetCode 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 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?

    题解:

    用HashMap<String, List<String>> hm 来保存每个file 的content 和对应的path集合.

    每个input string 按照 path fileName1(content1) fileName2(content2) 格式输入. 所以先按照空格断开,后面的都是文件名加上内容,再用"("断开提取内容.

    最后看内容对应文件数大于1的就是有duplicate.

    Time Complexity: O(paths.length * x). x为input string的平均长度.

    Space:O(paths.length * x). hm size.

    AC Java:

     1 class Solution {
     2     public List<List<String>> findDuplicate(String[] paths) {
     3         List<List<String>> res = new ArrayList<List<String>>();
     4         HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
     5         
     6         for(String path : paths){
     7             String [] pathArr = path.split("\s+");
     8             for(int i = 1; i<pathArr.length; i++){
     9                 String content = pathArr[i].substring(pathArr[i].indexOf("("));
    10                 String fileName = pathArr[i].substring(0, pathArr[i].indexOf("("));
    11                 List<String> list = hm.getOrDefault(content, new ArrayList<String>());
    12                 list.add(pathArr[0] + "/" + fileName);
    13                 hm.put(content, list);
    14             }
    15         }
    16         
    17         for(String key : hm.keySet()){
    18             if(hm.get(key).size() > 1){
    19                 res.add(hm.get(key));
    20             }
    21         }
    22         
    23         return res;
    24     }
    25 }
  • 相关阅读:
    使用pjsip传输已经编码的视频
    xubuntu14.04下编译pjsip及pjsua2 java
    解决 Python.h:没有那个文件或目录 错误的方法
    HIbernate與不支持boolean的數據庫之間的映射
    js数组的操作
    jQuery中ajax的使用与缓存问题的解决方法
    网络游戏中应用可插拔工厂处理消息
    How to Train Triplet Networks with 100K Identities?
    (转)如何用TensorLayer做目标检测的数据增强
    图像超分辨-IDN
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7720423.html
Copyright © 2011-2022 走看看