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
|
using namespace std;
typedef struct _Point {
int first_int;
int second_int;
} Point;
bool integerComp(const Point& p1, const Point& p2)
{
return p1.second_int > p2.second_int;
}
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> hashmap;
vector<int> ansVector;
vector<Point> countArray;
int nSize = (int)nums.size();
for (int i = 0; i < nSize; i++)
{
if (hashmap.count(nums[i]) == 0)
hashmap[nums[i]] = 1;
else
hashmap[nums[i]] = hashmap[nums[i]] + 1;
}
for (const auto& ele : hashmap)
{
Point eleInput;
eleInput.first_int = ele.first;
eleInput.second_int = ele.second;
countArray.emplace_back(eleInput);
}
sort(countArray.begin(), countArray.end(),integerComp);
for (int i = 0; i < k; i++)
{
ansVector.push_back(countArray[i].first_int);
}
return ansVector;
}
};
|
'C' 카테고리의 다른 글
c++ leetcode 525 contiguous array solution (0) | 2021.07.24 |
---|---|
c++ leetcode 380 Insert Delete GetRandom o(1) solution code (0) | 2021.07.17 |
c++ leetcode top k frequent words (sort and custom comparison) 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 |