【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\ ...
随机推荐
- 学习日记3、投机取巧使两个表的数据同时在一个treeGrid中显示
不多说了直接上代码, $('#List').treegrid({ url: '@Url.Action("GetList")', width: $(window).width() - ...
- 基于MyBatis实现Dao理论
基于MyBatis实现Dao理论 推荐使用xml提供sql 实现接口推荐使用Mapper自动实现DAO接口,让我们更关注sql书写本身
- composer国内镜像
composer国内镜像 一.总结 一句话总结: 直接百度 “composer 国内镜像” 即可 直接运行:composer config -g repo.packagist composer htt ...
- “The creator of this fault did not specify a Reason” Exception
“The creator of this fault did not specify a Reason” Exception if(!DidItPass) { InvalidRoutingCodeFa ...
- day38—JavaScript的运动基础-匀速运动
转行学开发,代码100天——2018-04-23 一.运动基础框架 JavaScript的运动可以广义理解为渐变效果,直接移动效果等,图网页上常见的“分享到”,banner,透明度变化等.其实现的基本 ...
- jmeter uniq 取值方式设置
- POJ1742 coins 动态规划之多重部分和问题
原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...
- 如何创建 Qt 插件?
如何创建 Qt 插件? 简单三部曲 定义接口类或接口基类并使用 Q_DECLARE_INTERFACE 宏进行声明 所有的插件都需要继承该基类并继承 QObject(不带界面插件) or QWidge ...
- eclipsePreferences位置
1.Windows:菜单栏-Window-Preferences 2.Mac:应用顶部最左侧Eclipse-Preferences ---------------------------------- ...
- Mysql一些概念,基本没啥用,
关系型数据库管理系统(RDBMS):是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据.特点:1.数据以表格的形式出现2.每行为各种记录名称3.每列为记录名称所对应的数 ...