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 time complexity must be better than O(n log n), where n is the array‘s size.

========

题目:

因为c++中提供的哈希表map,不能按照value进行排序,

我们需要一个按照value值进行排序的数据结构,所以需要自定义一个.

定义排序函数,static bool mykeyval(const keyval &l, const keyval &r){}

利用自带的sort()函数就可以了.

====

代码

class Solution {
public:
    struct keyval{
        int key;//数字
        int val;//数字出现的次数
        keyval(int k=0,int v=0):key(k),val(v){}
    };
    static bool mykeyval(const keyval &l,const keyval &r){
        return l.val>r.val;
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        map<int,int> m;///key,multis
        vector<int> re;
        for(auto i: nums){
            m[i]++;
        }
        vector<keyval> tmp(m.size());
        int d = 0;
        for(auto i: m){
            tmp[d].key = i.first;
            tmp[d++].val = i.second;
        }
        sort(tmp.begin(),tmp.end(),mykeyval);
        for(int i = 0;i<k;i++){
            re.push_back(tmp[i].key);
        }
        return re;
    }
};
时间: 2024-07-31 14:31:37

347. Top K Frequent Elements的相关文章

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

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

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

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

347. Top K Frequent Elements java solutions

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 (Medium)

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 前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 { pub