347 Top K Frequent Elements 前K个高频元素

给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
例如,
给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]。
注意:
    你可以假设给定的 k 总是合理的,1 ≤ k ≤ 数组中不相同的元素的个数。
    你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
详见:https://leetcode.com/problems/top-k-frequent-elements/description/
C++:

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> m;
        priority_queue<pair<int,int>> p;
        vector<int> res;
        for(int num:nums)
        {
            ++m[num];
        }
        for(auto a:m)
        {
            p.push({a.second,a.first});
        }
        for(int i=0;i<k;++i)
        {
            res.push_back(p.top().second);
            p.pop();
        }
        return res;
    }
};

参考:https://www.cnblogs.com/grandyang/p/5454125.html

原文地址:https://www.cnblogs.com/xidian2014/p/8836478.html

时间: 2024-08-02 15:18:54

347 Top K Frequent Elements 前K个高频元素的相关文章

[LeetCode] Top K Frequent Elements 前K个高频元素

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]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements.    Your algorithm's time complexity must be

LeetCode OJ 347. Top K Frequent Elements hashmap+排序求解

题目链接:https://leetcode.com/problems/top-k-frequent-elements/. 347. Top K Frequent Elements My Submissions QuestionEditorial Solution Total Accepted: 15510 Total Submissions: 36453 Difficulty: Medium Given a non-empty array of integers, return the k mo

[LeetCode][Python]Top K Frequent Elements

op 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]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time

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]. Note: 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/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]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

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]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet

347. Top K Frequent Elements/692. Top K Frequent Words

Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] 非常经典的一道题,有以下几点:1. 按照frequent 排序, 显然要建立 num : freq 的 map对, 定义 map_fre

[LC] 347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] class Solution { public List<Integer> topKFrequent(int[] nums, int

Java [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]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must