内容简介:题目地址:题目描述:
题目地址:
https://leetcode-cn.com/probl...
题目描述:
给定一个目录信息列表,包括目录路径,以及该目录中的所有包含内容的文件,您需要找到文件系统中的所有重复文件组的路径。一组重复的文件至少包括二个具有完全相同内容的文件。
输入列表中的单个目录信息字符串的格式如下:
"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"
这意味着有 n 个文件(f1.txt, f2.txt ... fn.txt 的内容分别是 f1_content, f2_content ... fn_content)在目录 root/d1/d2/.../dm 下。注意:n>=1 且 m>=0。如果 m=0,则表示该目录是根目录。
该输出是重复文件路径组的列表。对于每个组,它包含具有相同内容的文件的所有文件路径。文件路径是具有下列格式的字符串:
"directory_path/file_name.txt"
示例 1:
输入:
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
输出:
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
注:
最终输出不需要顺序。
您可以假设目录名、文件名和文件内容只有字母和数字,并且文件内容的长度在 [1,50] 的范围内。
给定的文件数量在 [1,20000] 个范围内。
您可以假设在同一目录中没有任何文件或目录共享相同的名称。
您可以假设每个给定的目录信息代表一个唯一的目录。目录路径和文件信息用一个空格分隔。
解答:
这一题实际上就是找重复文件名,文件名是括号里的内容,只需要把括号里的内容提取出来,存在hashmap中,
hashmap的键是文件名,值是文件名对应的路径的列表(List<String>)。
java ac代码:
class Solution { public List<List<String>> findDuplicate(String[] paths) { List<List<String>> ans = new ArrayList(200); HashMap<String,List<String>>map = new HashMap(500); for(String path:paths) { String[] temp = path.split(" "); String root = temp[0]; for(int i = 1;i < temp.length;i++) { int begin = temp[i].indexOf("("); int end = temp[i].lastIndexOf(")"); String name = temp[i].substring(begin+1,end); String s = root+"/"+temp[i].substring(0,begin); if(map.get(name) == null) { List<String> list = new ArrayList(10); list.add(s); map.put(name,list); } else map.get(name).add(s); } } for(Map.Entry<String,List<String> > entry:map.entrySet()) if(entry.getValue().size()>1) ans.add(entry.getValue()); return ans; } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Release It!
Michael T. Nygard / Pragmatic Bookshelf / 2007-03-30 / USD 34.95
“Feature complete” is not the same as “production ready.” Whether it’s in Java, .NET, or Ruby on Rails, getting your application ready to ship is only half the battle. Did you design your system to......一起来看看 《Release It!》 这本书的介绍吧!