面试题30_相关题目(最小的k个数_方法二)
来源:互联网 发布:大数据时代的算法 编辑:程序博客网 时间:2024/05/20 19:48
代码示例:
#include<iostream>#include<set>#include<iterator>#include<functional> #include <algorithm> using namespace std;typedef multiset<int, greater<int> > IntSet;typedef multiset<int, greater<int> >::iterator setIterator;void FindKSmall(int a[], int n, int k,IntSet&res){if (a == NULL || n <= 0 || k > n||k<=0)return;res.clear();int index;for (int index = 0; index < n; index++){if (res.size() < k){res.insert(a[index]);}else{setIterator it = res.begin();if (a[index] < *it){res.erase(it);res.insert(a[index]);}}}}int main(){const int n = 10;int k = 5;int a[n] = { 7, 3, 2, 6, 9, 0, 0, 10, 32, 5 };cout << "原数组:";for (int i = 0; i < n; i++)cout << a[i] << " ";cout << endl;IntSet res;cout << "最小的" << k << "个数:";FindKSmall(a, n, k,res);for (setIterator it=res.begin(); it!=res.end(); it++)cout << *it << " ";cout << endl;}
阅读全文
0 0
- 面试题30_相关题目(最小的k个数_方法二)
- 剑指Offer_面试题30_最小的k个数
- 九度_题目1371:最小的K个数
- 面试题30:最小的k个数
- 面试题30 最小的K个数
- 面试题30:最小的K个数
- 面试题30 最小的K个数
- 面试题30:最小的K个数
- 面试题30:最小的k个数
- 面试题30:最小的K个数
- 面试题30:最小的k个数
- 面试题30最小的k个数
- 面试题30:最小的k个数
- 面试题30:最小的k个数
- 面试题 30 : 最小的K个数
- 最小的K个数(面试题 30)
- 面试题30:最小的k个数(未)
- 面试题26:最小的k个数
- ListView的position含义
- django cache缓存数据
- 迅为开发板 4418开发板 can测试内容分享
- HTML5本地缓存manifest与localstorage 结合使用方式
- 深入理解操作系统原理之操作系统概述
- 面试题30_相关题目(最小的k个数_方法二)
- 图像和流媒体 -- I 帧,B帧,P帧,IDR帧的区别
- HDU1663The Counting Problem
- hashids 加密 id
- Codeforces 821A Okabe and Future Gadget Laboratory 题解
- html中select标签刷新后不回到默认值而是保持之前选择值
- 1、JDK初步与HelloWorld
- JQuery banner轮播
- 内存泄露详解