LeetCode 347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.

分析:先遍历数组将数组的值与出现的次数用map映射,因为根据map的值来排序,所以用pair<int, int>类型的vector转存后用sort实现排序方法。从大到小排序,然后取前k个值储存在新的vector v中返回v。