609. Find Duplicate File in System

寻找重复文件的路径集合

/*
        没有什么特别的技巧,就是用map存储相同内容的路径,核心就是getOrDefault()方法
        注意步骤,想清楚思路
         */
        //记录结果
        Map<String,ArrayList> map = new HashMap<>();
        for (String path :
                paths) {
            //把根路径和各个文件分开
            String[] files = path.split(" ");
            //遍历每个文件,有两个任务,一是存下改文件内容,二是把该文件的路径记录下来
            for (int i = 1; i < files.length; i++) {
                String file = files[i];
                //记录文件路径的容器,由于要不断添加,所以选择可以改变的StringBulider
                StringBuilder temp = new StringBuilder();
                //添加根路径
                temp.append(files[0]);
                //别忘了跟路径后边要有个/
                temp.append(‘/‘);
                //记录从什么地方开始内容的开始
                int j;
                for (j= 0; j < file.length(); j++) {
                    if (file.charAt(j)!=‘(‘)
                    {
                        //只要不是内容开始的标志(‘(‘)就把路径添加
                        temp.append(file.charAt(j));
                    }
                    else break;
                }
                //记录下内容
                String con = file.substring(j+1,file.length()-1);
                //得到该内容的list,如果没有的话就新建一个
                ArrayList list = map.getOrDefault(con,new ArrayList());
                //添加上新的相同内容路径
                list.add(new String(temp));
                //重新加入到map
                map.put(con,list);
            }
        }
        //下面的工作的就是把map中存的list添加到结果中
        List<List<String>> res = new ArrayList<>();
        for (String s:
                map.keySet()) {
            //注意这里有个小陷阱,如果没有重复的话,就不添加这个list
            if (map.get(s).size()>1)
            res.add(map.get(s));
        }
        return res;

原文地址:https://www.cnblogs.com/stAr-1/p/8284534.html

时间: 2024-11-10 16:51:30

609. Find Duplicate File in System的相关文章

609. Find Duplicate File in System(LeetCode)

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 l

[LeetCode] 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 l

兔子--Error generating final archive: Found duplicate file for APK: LICENSE

Error generating final archive: Found duplicate file for APK: LICENSE Origin 1: E:\work\e\libs\mockito-all-1.9.5.jar Origin 2: E:\work\e\libs\mockito-core-1.9.5-sources.jar 有重复的jar包

??Duplicate File Finder Pro mac(重复文件搜索软件)

?Duplicate File Finder Pro mac特别版是Mac平台上的一款好用的重复文件搜索软件,操作简单,使用方便,只需要三步就可以快速查找和删除重复文件,重复文件通常是浪费和不必要的,保持它们只是浪费宝贵的硬盘空间.Duplicate File Finder Pro mac能够轻松的为你的电脑删除隐藏着的重复文件.减轻电脑的内存空间的压力,加快运行速度. ?Duplicate File Finder Pro mac特别版下载路径:http://t.cn/AiYiK0Lw ?Dup

Duplicate File Finder Remover for Mac(重复文件查找软件) v1.2.5569中文

你是否还在为内存中太多的重复文件占用空间而烦恼呢?试试Duplicate File Finder Remover for Mac吧!Duplicate File Finder Remover Mac版是一款运行在MacOS平台上的重复文件查找软件.Mac电脑每个硬盘驱动器每天都会收到很多的重复文件,内存被重复的照片,歌曲,文档,视频挤满.Duplicate File Finder Remover可以帮你自动或手动删除不需要的重复文件,清理内存空间. Duplicate File Finder R

File类--System.out.print(Object obj)的理解

一.File 类(java.io) 概述:Java中使用File类来表示文件或者文件夹对象!     抽象路径名:描述文件或文件夹时,使用的路径符号,就是一个对象的字符串表示形式,如"c:\\";     绝对路径:绝对位置开始的路径;     相对路径:相对位置开始的路径; 构造方法:     File(String pathname)     File(String parent, String child)     File(File parent, String child)

Android File Hierarchy : System Structure Architecture Layout

Most of the Android user are using their Android phone just for calls, SMS, browsing and basic apps, But form the development prospective, we should know about  Android internal structure. Android uses several partitions (like boot, system, recovery,

poj 3836 P2P File Sharing System

这题是一个模拟p2p的网络的一个题目.题意比较繁琐,只要看懂课,细心一点就很好AC了.直接上代码了,存模拟. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include<algorithm> #include<v

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea