code >>
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
|
using namespace std;
typedef struct _Point {
int second_int;
string first_string;
} Point;
bool comp(const Point& p1, const Point& p2)
{
if (p1.second_int > p2.second_int)
{
return true;
}
else if (p1.second_int == p2.second_int)
{
return p1.first_string < p2.first_string;
}
else
return false;
}
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> hashmap;
vector<Point> countArray;
vector<string> ansVector;
int wSize = (int)words.size();
for (int i = 0; i < wSize; i++)
{
if (hashmap.count(words[i]) == 0)
hashmap[words[i]] = 1;
else
hashmap[words[i]] = hashmap[words[i]] + 1;
}
for (const auto& ele : hashmap)
{
Point eleInput;
eleInput.first_string = ele.first;
eleInput.second_int = ele.second;
countArray.emplace_back(eleInput);
}
sort(countArray.begin(), countArray.end(), comp);
for (int i = 0; i < k; i++)
{
ansVector.push_back(countArray[i].first_string);
}
return ansVector;
}
};
|
'C' 카테고리의 다른 글
c++ leetcode 380 Insert Delete GetRandom o(1) solution code (0) | 2021.07.17 |
---|---|
c++ leetcode top-k frequent elements O(nlogn) solution (0) | 2021.07.16 |
c++ leetcode 290 word pattern with double hashmap(100% faster) solution (0) | 2021.07.16 |
c++ leetcode 739 daily temperature code (0) | 2021.07.13 |
c++ leet code 227, Basic CalculatorII code (0) | 2021.07.12 |