【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

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

C++

//问题:返回数组中出现频次前k大的元素
using namespace std;
#include <algorithm>
#include <unordered_map>
class Solution
{
public:
    vector<int> topKFrequent(vector<int>& a, int k)
    {
        unordered_map<int,int> counter; //出现频次计数器map 
        priority_queue<pair<int, int>> q; //注意<pair<int, int>>的用法
        vector<int> res; //用于存储结果
       
        for(auto key:a) counter[key]++; //用counter[key]访问map中为key的value,按<数字,频次> 组织map
        for(auto it:counter) q.push({it.second, it.first}); //注意使用{}对,按<频次,数字> 组织,构建一个大顶堆(默认)
        for(int i=0;i<k; i++)//输出前k个最大值
        {
            res.push_back(q.top().second);
            q.pop();
        }
        return res;
       
        //时间分析:
        //构建map耗时O(n),构建大顶堆O(nlogn),输出前k个数O(klogn),时间复杂度O(nlogn)
    }
};
/*无法做出
//计数得统计“直方图”,再用堆排序找前k个元素
#include <algorithm>
class Solution
{
public:
    vector<int> topKFrequent(vector<int>& a, int k)
    {
        vector<int> count(k); //出现频次计数器
        for(int i=0; i < a.size(); i++) //频次计数,一次遍历,时间复杂度为O(n)
        {
            count[a[i]]++;
        }
       
        partial_sort(count.begin(), count.begin()+k, count.end(), greater<int>()); //堆排序,时间复杂度为O(klogn)
        //注意默认为增序排列,这里要找前k大的数,故比较函数应设置为greater
       
       
   
        //程序时间复杂度为O(n+klogn)
    }
};*/

转载于:https://www.cnblogs.com/wikiwen/p/10225959.html

【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements相关推荐

  1. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  2. 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Wiggle Sort II Given an unsorted array nums, reorder it su ...

  3. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  4. 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...

  5. 【LeetCode 剑指offer刷题】树题19:8 二叉树中序遍历的下一个结点

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 8 二叉树中序遍历的下一个结点 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注 ...

  6. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  7. 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...

  8. 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...

  9. 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...

最新文章

  1. 先考一键还是先学java_是先考C还是先考JAVA
  2. 物体的识别,检测,和分割
  3. 2017-2018-1 20155226 20155234 《信息安全系统设计基础》 实验一总结
  4. IOS 9人机界面指南(1):UI设计基础
  5. MyBatis 缓存详解-开启二级缓存的方法
  6. webpack4.x最详细入门讲解
  7. indexOf 方法
  8. hadoop-集群管理(3)——不常用参数
  9. Using a Comparison Function for the Key Type
  10. 分布式存储---moosefs部署
  11. linux进程互斥等待
  12. html弹框整体缩放,网页弹出对话框无法放大
  13. postgresql如何优雅地清理磁盘空间碎片VACUUM
  14. Ubuntu 旅行日记 Day 1
  15. python爬虫GUI工具,tkinter网易云歌单歌曲下载器
  16. 2021.11.19【读书笔记】丨snakemake常见问题汇总(下)
  17. 全国关于省市区/县的行政区划数据-数据来源国家统计局
  18. 用php求两数之和,Leetcode PHP 两数之和
  19. 如何取消计算机中的文件夹加密文件,文件夹加密码怎么设置怎么解除
  20. wms地图绘制工具_WMS/WMTS

热门文章

  1. NPM是什么?我们该怎么使用呢
  2. python基本数据类型及语法
  3. google的几个搜索业务
  4. 前向传播算法和反向传播算法
  5. matlab将矩阵分解成lu,10行代码实现矩阵的LU分解(matlab)
  6. 数值分析:插值与拟合
  7. 神经网络为什么需要随机初始化模型参数
  8. 深度学习的实用层面 —— 1.5 为什么正则化可以减少过拟合
  9. 无人驾驶二 卡尔曼滤波与PID控制
  10. VBA 读写文本文件的几种方法