ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【leetcode】347. Top K Frequent Elements

2019-11-30 21:01:36  阅读:299  来源: 互联网

标签:numVec Elements int Top iter length 347 numMap vector


题目地址:https://leetcode.com/problems/top-k-frequent-elements/

从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决。

关于堆排序,其时间复杂度在最好和最坏的场景下都是O(nlogn)。

一开始想定义一个结构体,包含元素和元素个数两个成员,后直接用pair存储即可。

 

解题思路:

1. 分别统计每个数字的个数,建立数字和个数的映射,用pair存储,first=数字个数,second=数字,然后存入集合。

2. 以不同数字的个数建立大顶堆。

3.调整K次堆,依次得到K个出现次数最多的数字。

 

代码:

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        vector<int> topK;
        if (nums.size() == 0) {
            return topK;
        }
        vector<pair<int, int>> numVec;
        map<int, int> numMap;
        for(int num : nums) {
            if (numMap.count(num)) {
                numMap[num]++;
            } else {
                numMap[num] = 1;
            }
        }
        
        map<int, int>::iterator iter;
        iter = numMap.begin();
        while(iter != numMap.end()) {
            numVec.push_back(pair<int, int>(iter->second, iter->first));
            iter++;
        }
        int length = (int) numVec.size();
        for (int i = length / 2 - 1; i >= 0; i --) {
            sift(i, length - 1, numVec);
        }

        for (int i = length - 1; i > length - k - 1;  i --) {
            topK.push_back(numVec[0].second);
            swap(numVec[0], numVec[i]);
            sift(0, i - 1, numVec);
        }
        
        return topK;
    }
    
    void sift(int low, int high, vector<pair<int, int>> &numVec) {
        int i = low, j = 2 * i + 1;
        while (j <= high) {
            if (j < high && numVec[j].first < numVec[j+1].first) {
                j++;
            }
            
            if (numVec[i].first < numVec[j].first) {
                swap(numVec[i], numVec[j]);
                i = j;
                j = 2 * i + 1;
            } else {
                break;
            }
        }
    }

};

 

标签:numVec,Elements,int,Top,iter,length,347,numMap,vector
来源: https://www.cnblogs.com/AndrewGhost/p/11963912.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有