【leetcode】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.txt,f2.txt...fn.txtwith contentf1_content,f2_content...fn_content, respectively) in directoryroot/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.
 
解题思路:我的方法非常简单,解析输入的字符串,把文件内容作为key,文件路径作为value存入字典中,当然value是用list保存的。最后遍历字典,value超过两个元素即为符合条件的结果。
代码如下:
class Solution(object):
def findDuplicate(self, paths):
"""
:type paths: List[str]
:rtype: List[List[str]]
"""
dic = {}
for p in paths:
item = p.split(' ')
for i in range(1,len(item)):
file_name = item[i].split('(')[0]
file_content = item[i].split('(')[1][:-1]
dic[file_content] = dic.setdefault(file_content,[]) + [item[0] + '/' + file_name]
res = []
for v in dic.itervalues():
if len(v) >= 2:
res.append(v)
return res
【leetcode】609. Find Duplicate File in System的更多相关文章
- 【LeetCode】609. Find Duplicate File in System 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
		
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
 - 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 ...
 - 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】316. Remove Duplicate Letters 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
 - 【LeetCode】217. Contains Duplicate (2 solutions)
		
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
 - 【leetcode】316. Remove Duplicate Letters
		
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
 - 【leetcode】388. Longest Absolute File Path
		
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
 
随机推荐
- Python分布式爬虫打造搜索引擎完整版-基于Scrapy、Redis、elasticsearch和django打造一个完整的搜索引擎网站
			
Python分布式爬虫打造搜索引擎 基于Scrapy.Redis.elasticsearch和django打造一个完整的搜索引擎网站 https://github.com/mtianyan/Artic ...
 - 万能的gitignore文件模版
			
## .gitignore for Grails 1.2 and 1.3 # .gitignore for maven target/ *.releaseBackup # web applicatio ...
 - How to: Create a Windows Communication Foundation Client
			
How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...
 - 穿戴-智能穿戴-ProjectGlass:谷歌眼镜(Google Project Glass)
			
ylbtech-穿戴-智能穿戴-ProjectGlass:谷歌眼镜(Google Project Glass) 谷歌眼镜(Google Project Glass)是由谷歌公司于2012年4月发布的一 ...
 - RichEdit  学习
			
procedure TForm1.AddText(RichEdit: TRichEdit; Str: string; TextColor: TColor = clBlack; FontName: st ...
 - java   虚方法。     后面new  那个类, 就调用哪个类的方法 ,而非定义类的方案。  关于父子 类的   呵呵
			
java 虚方法. 后面new 那个类, 就调用哪个类的方法 ,而非定义类的方案. 关于父子 类的 呵呵 在多态的情况下,声明为父类类型的引用变量只能调用父类中的方法,但如果此变量 ...
 - [COCI2017.1]Deda —— 解锁线段树的新玩法
			
众所周知,能用线段树做的题一定可以暴力 但考场上也只能想到暴力了,毕竟还是对线段树不熟练. deda 描述 有一辆车上有n个小孩,年龄为1~n,然后q个询问,M X A代表在第X站时年龄为A的小孩会下 ...
 - js 解决函数加载的问题
			
var queue = function(funcs, scope) { (function next() { if(funcs.length > 0 ...
 - Spring学习(五)--构建Spring Web应用程序
			
一.Spring MVC起步 看过猫和老鼠的小伙伴都可以想象Tom猫所制作的捕鼠器:它的目标 是发送一个小钢球,让它经过一系列稀奇古怪的装置,最后触发捕鼠 器.小钢球穿过各种复杂的配件,从一个斜坡上滚 ...
 - Struts2+Ajax实现数据交互
			
1.导入jar包 struts核心包: json需要的包: 2.配置web.xml <filter> <filter-name>struts2</filter-name& ...