日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

LeetCode 609. 在系统中查找重复文件(哈希)

發(fā)布時(shí)間:2024/7/5 windows 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 609. 在系统中查找重复文件(哈希) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 題目

給定一個(gè)目錄信息列表,包括目錄路徑,以及該目錄中的所有包含內(nèi)容的文件,您需要找到文件系統(tǒng)中的所有重復(fù)文件組的路徑。
一組重復(fù)的文件至少包括二個(gè)具有完全相同內(nèi)容的文件。

輸入列表中的單個(gè)目錄信息字符串的格式如下:

"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"

這意味著有 n 個(gè)文件(f1.txt, f2.txt ... fn.txt 的內(nèi)容分別是 f1_content, f2_content ... fn_content)在目錄 root/d1/d2/.../dm 下。注意:n>=1 且 m>=0。如果 m=0,則表示該目錄是根目錄。

該輸出是重復(fù)文件路徑組的列表。
對(duì)于每個(gè)組,它包含具有相同內(nèi)容的文件的所有文件路徑。
文件路徑是具有下列格式的字符串:

"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"]]注: 最終輸出不需要順序。 您可以假設(shè)目錄名、文件名和文件內(nèi)容只有字母和數(shù)字,并且文件內(nèi)容的長(zhǎng)度在 [150] 的范圍內(nèi)。 給定的文件數(shù)量在 [120000] 個(gè)范圍內(nèi)。 您可以假設(shè)在同一目錄中沒(méi)有任何文件或目錄共享相同的名稱。 您可以假設(shè)每個(gè)給定的目錄信息代表一個(gè)唯一的目錄。目錄路徑和文件信息用一個(gè)空格分隔。超越競(jìng)賽的后續(xù)行動(dòng):假設(shè)您有一個(gè)真正的文件系統(tǒng),您將如何搜索文件?廣度搜索還是寬度搜索? 如果文件內(nèi)容非常大(GB級(jí)別),您將如何修改您的解決方案? 如果每次只能讀取 1 kb 的文件,您將如何修改解決方案? 修改后的解決方案的時(shí)間復(fù)雜度是多少? 其中最耗時(shí)的部分和消耗內(nèi)存的部分是什么?如何優(yōu)化? 如何確保您發(fā)現(xiàn)的重復(fù)文件不是誤報(bào)?

來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/find-duplicate-file-in-system
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

2. 解題

class Solution { // C++ public:vector<vector<string>> findDuplicate(vector<string>& paths) {unordered_map<string, unordered_set<string>> m;//文件內(nèi)容, 文件路徑集合string content, path, file;for(auto& p : paths) {content = path = file = "";int i = p.find(' ');path = p.substr(0,i)+"/";//路徑bool foundcontent = false;for(i++; i < p.size(); ++i){if(p[i] == '('){foundcontent = true;continue;}if(p[i] == ')'){m[content].insert(path+file);//記錄內(nèi)容包含的路徑文件foundcontent = false;content = file = "";i++;//跳過(guò)空格continue;}if(!foundcontent)file += p[i];elsecontent += p[i];}}vector<vector<string>> ans;for(auto& mi : m){if(mi.second.size() >= 2)ans.push_back(vector<string>(mi.second.begin(), mi.second.end()));}return ans;} };

212 ms 36.1 MB

class Solution:# py3def findDuplicate(self, paths: List[str]) -> List[List[str]]:m = {};for p in paths:i = p.find(' ')content, path, file = "","",""path = p[0:i]+'/'foundcontent = Falsei += 1while i < len(p):if p[i]=='(':foundcontent = Truei += 1continueif p[i]==')':if content not in m:m[content] = set()m[content].add(path+file)foundcontent = Falsecontent, file = "", ""i += 2continueif not foundcontent:file += p[i]else:content += p[i]i += 1ans = []for content in m:if len(m[content]) >= 2:ans.append(list(m[content]))return ans

332 ms 25.7 MB

總結(jié)

以上是生活随笔為你收集整理的LeetCode 609. 在系统中查找重复文件(哈希)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。