?LeetCode刷題實(shí)戰(zhàn)609:在系統(tǒng)中查找重復(fù)文件

示例
示例 1:
輸入:paths = ["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"]]
示例 2:
輸入:paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"]
輸出:[["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]]
解題




HashMap< String, List < String >> map?= new?HashMap < > ();
????????for?(String path: paths) {
????????????String[] values = path.split(" ");
????????????for?(int?i = 1; i < values.length; i++) {
????????????????String[] name_cont = values[i].split("\\(");
????????????????name_cont[1] = name_cont[1].replace(")", "");
????????????????List < String > list?= map.getOrDefault(name_cont[1], new?ArrayList < String > ());
????????????????list.add(values[0] + "/"?+ name_cont[0]);
????????????????map.put(name_cont[1], list);
????????????}
????????}
????????List < List < String >> res = new?ArrayList< >();
????????for?(String key: map.keySet()) {
????????????if?(map.get(key).size() > 1)
????????????????res.add(map.get(key));
????????}
????????return?res;
評論
圖片
表情
