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.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:
- No order is required for the final output.
- 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].
- The number of files given is in the range of [1,20000].
- You may assume no files or directories share the same name in the same directory.
- 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:
- Imagine you are given a real file system, how will you search files? DFS or BFS?
- If the file content is very large (GB level), how will you modify your solution?
- If you can only read the file by 1kb each time, how will you modify your solution?
- 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?
- 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:
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
List<List<String>> res = new ArrayList<List<String>>();
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
for(String path : paths){
String [] pathArr = path.split("\\s+");
for(int i = 1; i<pathArr.length; i++){
String content = pathArr[i].substring(pathArr[i].indexOf("("));
String fileName = pathArr[i].substring(0, pathArr[i].indexOf("("));
List<String> list = hm.getOrDefault(content, new ArrayList<String>());
list.add(pathArr[0] + "/" + fileName);
hm.put(content, list);
}
}
for(String key : hm.keySet()){
if(hm.get(key).size() > 1){
res.add(hm.get(key));
}
}
return res;
}
}
LeetCode Find Duplicate File in System的更多相关文章
- [LeetCode] Find Duplicate File in System 在系统中寻找重复文件
Given a list of directory info including directory path, and all the files with contents in this dir ...
- LC 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- 【LeetCode】609. Find Duplicate File in System 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】609. Find Duplicate File in System
题目如下: Given a list of directory info including directory path, and all the files with contents in th ...
- [Swift]LeetCode609. 在系统中查找重复文件 | Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- [leetcode-609-Find Duplicate File in System]
https://discuss.leetcode.com/topic/91430/c-clean-solution-answers-to-follow-upGiven a list of direct ...
- 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
随机推荐
- Saltstack 命令行:批量发送命令,返回执行结果
批量发送发送命令符,并返回结果. salt '*' cmd.run 'df -h' ---------------------------------------- Stest1: Filesyste ...
- jQuery二级下拉菜单
在线演示 本地下载
- CSS3自定义发光radiobox单选框
在线演示 本地下载
- SpringBoot 定义通过字段验证
第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- tomcat配置访问图片路径映射到磁盘路径
首先,我在调试页面的时候发现,图片路径为: /webapps/pic_son/img/1234565456.jpg 但是,tomcat中webapps的文件夹下的pic_son项目中,img文件夹是空 ...
- Pandas排序
Pandas有两种排序方式,它们分别是 - 按标签 按实际值 下面来看看一个输出的例子. import pandas as pd import numpy as np unsorted_df=pd.D ...
- TCP_AIO_Server_ZC_02
ZC: 这个例子是,1个skt 投递 N个未决的接收操作 (记得 以前查过 说 线程数是 CUP数量的2倍 比较合适) 1. // 当需要 投递多个接收操作的时候,可以将接收缓冲封装成类,然后再投递多 ...
- GDI+ 双缓冲字体模糊
只是记录自己的UI库,对其他估计没什么帮助 void CListCtrlUI::ReFillRect(HDC hdc){ if (!m_pImage) { Graphics gs(hdc); int ...
- svn官方版本的使用
创建仓库的命令是:svndadmin create c:\abcde 启动命令是:svnserve -d -r c:\abcde 官方版本,svn路径