leetcode 347. Top K Frequent Elements

来源:互联网 发布:简谱制作软件app 编辑:程序博客网 时间:2024/05/22 19:54
/*leetcode 347. Top K Frequent ElementsGiven 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].Note: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.题目大意:对于给定的数组,找到出现次数最多的k个元素解题思路:1. 先统计每个元素出现的次数2. 类似于hashtable,对于每一种次数,都有一个bucket。一个bucket为一个数组,存放出现次数为相同的元素。然后找出这k个元素即可20 / 20 test cases passed.Status: AcceptedRuntime: 36 ms*/#include <iostream>#include <vector>#include <string>#include <algorithm>#include <unordered_map>using namespace std;class Solution {public:    vector<int> topKFrequent(vector<int>& nums, int k) {        //count every element in nums        unordered_map<int, int> umap;        for (auto i : nums)            ++umap[i];        //按照出现的次数放在buckets中        vector<vector<int>>buckets(nums.size());        for (auto iter = umap.begin(); iter != umap.end(); ++iter)        {            buckets[iter->second - 1].push_back(iter->first);        }        vector<int> res;        //统计最大的k个数        for (int i = buckets.size() - 1; i >= 0; --i)        {            for (int j = 0; j < buckets[i].size(); ++j)            {                res.push_back(buckets[i][j]);                if (res.size() == k)                    return res;            }        }        return res;    }};void test(){    Solution sol;    vector<int> v{ 1,1,1,2,2,3 };    vector<int> ret =  sol.topKFrequent(v, 2);    for (auto i : ret)        cout << i << " ";    cout << endl;}int main(){    test();    return 0;}
0 0
原创粉丝点击