-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path609.cpp
More file actions
74 lines (74 loc) · 2.18 KB
/
609.cpp
File metadata and controls
74 lines (74 loc) · 2.18 KB
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
// hash+bettersort+102ms.cpp
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string> &paths) {
unordered_map<string, int> hashmap;
stringstream ss;
string path, filename, content;
vector<vector<string>> res;
for (string str : paths) {
ss << str;
ss >> path;
while (!ss.eof()) {
ss >> filename;
int left = filename.find("("), right = filename.find(")");
content = filename.substr(left, right - left);
// filename=filename.substr(0,left);
if (hashmap.find(content) == hashmap.end()) {
hashmap[content] = res.size();
res.push_back(
vector<string>(1, path + "/" + filename.substr(0, left)));
} else
res[hashmap[content]].emplace_back(path + "/" +
filename.substr(0, left));
}
ss.clear();
}
int i = 0, j = res.size() - 1;
while (i <= j) {
while (i <= j && res[i].size() != 1)
++i;
while (i <= j && res[j].size() == 1)
--j;
if (i <= j)
res[i++] = res[j--];
}
res.resize(j + 1);
return res;
}
};
// hash+sort+96ms.cpp
class Solution2 {
public:
vector<vector<string>> findDuplicate(vector<string> &paths) {
unordered_map<string, int> hashmap;
stringstream ss;
string path, filename, content;
vector<vector<string>> res;
for (string str : paths) {
ss << str;
ss >> path;
while (!ss.eof()) {
ss >> filename;
int left = filename.find("("), right = filename.find(")");
content = filename.substr(left, right - left);
filename = filename.substr(0, left);
if (hashmap.find(content) == hashmap.end()) {
hashmap[content] = res.size();
res.push_back(vector<string>(1, path + "/" + filename));
} else
res[hashmap[content]].emplace_back(path + "/" + filename);
}
ss.clear();
}
sort(res.begin(), res.end(), [](vector<string> &a, vector<string> &b) {
return a.size() > b.size();
});
int i;
for (i = res.size() - 1; i >= 0 && res[i].size() == 1; --i)
;
if (i >= -1)
res.resize(i + 1);
return res;
}
};