FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_26/Leetcode_609_26.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_02
/
id_26
/
Leetcode_609_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (69 loc) · 2.69 KB
Breadcrumbs
algorithm
/
Week_02
/
id_26
/
Leetcode_609_26.java
Copy path
File metadata and controls
80 lines (69 loc) · 2.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package
com
.
fanlu
.
leetcode
.
hashtable
;
// Source : https://leetcode.com/problems/find-duplicate-file-in-system/
// Id : 609
// Author : Fanlu Hai
// Date : 2018-04-23
// Other :
// Tips :
import
java
.
util
.*;
/**
* 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"]]
*/
public
class
FindDuplicateFileInSystem
{
Map
<
String
,
ArrayList
<
String
>>
map
=
new
HashMap
<>();
//75.63% 20.59%
public
List
<
List
<
String
>>
findDuplicateOriginal
(
String
[]
paths
) {
for
(
String
s
:
paths
) {
getContentMapFromPath
(
s
);
}
//! below codes cause concurrent modification exception, use iterator instead
// for(String key : map.keySet()){
// if (map.get(key).size()==1)
// map.remove(key);
// }
Iterator
<
Map
.
Entry
<
String
,
ArrayList
<
String
>>>
iterator
=
map
.
entrySet
().
iterator
();
while
(
iterator
.
hasNext
()) {
Map
.
Entry
<
String
,
ArrayList
<
String
>>
entry
=
iterator
.
next
();
if
(
entry
.
getValue
().
size
() ==
1
) {
iterator
.
remove
();
}
}
return
new
ArrayList
<
List
<
String
>>(
map
.
values
());
}
public
void
getContentMapFromPath
(
String
path
) {
String
[]
tmp
=
path
.
split
(
" "
);
String
prefix
=
tmp
[
0
] +
"/"
;
for
(
int
i
=
1
;
i
<
tmp
.
length
;
i
++) {
String
[]
foo
=
tmp
[
i
].
split
(
"
\\
("
);
// use content value as key, an arrayList of paths as value.
String
key
=
foo
[
1
].
substring
(
0
,
foo
[
1
].
length
() -
1
);
ArrayList
<
String
>
value
=
map
.
getOrDefault
(
key
,
new
ArrayList
<
String
>());
value
.
add
(
prefix
+
foo
[
0
]);
map
.
put
(
key
,
value
);
}
}
//63.86% 76.47%
public
List
<
List
<
String
>>
findDuplicate
(
String
[]
paths
) {
Map
<
String
,
Set
<
String
>>
map
=
new
HashMap
<>();
for
(
String
path
:
paths
) {
String
[]
tmp
=
path
.
split
(
" "
);
for
(
int
i
=
1
;
i
<
tmp
.
length
;
i
++) {
String
[]
foo
=
tmp
[
i
].
split
(
"
\\
("
);
// use content value as key, an arrayList of paths as value.
String
key
=
foo
[
1
].
substring
(
0
,
foo
[
1
].
length
() -
1
);
Set
<
String
>
value
=
map
.
getOrDefault
(
key
,
new
HashSet
<
String
>());
value
.
add
(
tmp
[
0
] +
"/"
+
foo
[
0
]);
map
.
put
(
key
,
value
);
}
}
List
<
List
<
String
>>
result
=
new
ArrayList
<
List
<
String
>>();
for
(
Set
<
String
>
list
:
map
.
values
()) {
if
(
list
.
size
() !=
1
)
result
.
add
(
new
ArrayList
<>(
list
));
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL