面试题 收集请求k千里马
2015-07-24 08:34
459 查看
收集请求k最大值
个人信息:就读于燕大本科软件project专业 眼下大三;
本人博客:google搜索“cqs_2012”就可以;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客时间:2014-5-15;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008;
制图工具:office 2010 powerpoint;
硬件信息:7G-3 笔记本;
真言
是问题就有方法去解决。
题目
集合里求出k个最大值。
思路
利用堆的特性去解决
建立K+1规模的小顶堆。然后不断更新堆顶,直至更新完,最后堆里除了堆顶外就是k个最大值
代码
主函数
heap_make 函数
heap_downcast 函数
heap_upcast 函数
个人信息:就读于燕大本科软件project专业 眼下大三;
本人博客:google搜索“cqs_2012”就可以;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客时间:2014-5-15;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008;
制图工具:office 2010 powerpoint;
硬件信息:7G-3 笔记本;
真言
是问题就有方法去解决。
题目
集合里求出k个最大值。
思路
利用堆的特性去解决
建立K+1规模的小顶堆。然后不断更新堆顶,直至更新完,最后堆里除了堆顶外就是k个最大值
代码
主函数
std::pair<int *,int> KMaxOfArray(int * data,int const length,int K) { // 创建k+1规模的堆 int * pheap = new int[K+1]; for(int i = 0;i<K+1;i++) { pheap[i] = data[i]; } Heap<int>::Heap_make(pheap,K+1,Heap<int>::Heap_min); // 遍历整个数组。然后得到K+1个最大数 for(int i = K+1;i<length;i++ ) { pheap[0] = data[i]; Heap<int>::Heap_downcast(pheap,0,K+1,Heap<int>::Heap_min); } // 返回结果 return std::make_pair(pheap+1,K); }
heap_make 函数
// 建堆操作 4 template<typename T> void Heap<T>::Heap_make(T *data,int const length,bool (*pf)(T,T)) { // 异常输入 if( data == NULL || length < 0 ) cout<<"异常输入"<<endl; else // 正常输入 for(int i=0;i<length;i++) { Heap_upcast(data,i,length,pf); } };
heap_downcast 函数
// 堆得下溯操作 template<typename T> void Heap<T>::Heap_downcast(T *data,int i,const int length,bool (*pf)(T,T)) { if(data != NULL && length >= 0) { T max ; // have two children while(i*2+2 <length) { max = data[i]; //if(max >= data[i*2+1] && max >= data[2*i+2]) if(pf(data[i*2+1],max) == false && pf(data[i*2+2],max) == false) break; // right child bigger //if( data[i*2+2]>data[2*i+1] && data[i*2+2]>max) if(pf(data[i*2+2],data[2*i+1]) == true && pf(data[i*2+2],max) == true) { max = data[i*2+2]; data[i*2+2] = data[i]; data[i] = max; i = i*2+2; } // left child bigger //else if( data[i*2+1] >= data[2*i+2] && data[i*2+1]>max ) else if(pf(data[i*2+2] ,data[2*i+1]) == false && pf(data[i*2+1],max) == true) { max = data[i*2+1]; data[i*2+1] = data[i]; data[i] = max; i = i*2+1; } } // have one child if(i*2+1 < length) { //if(data[i*2+1]>data[i]) if(pf(data[i*2+1],data[i]) == true) { max = data[i*2+1]; data[i*2+1] = data[i]; data[i] = max; } } } else { cout<<"exception of input Heap_downcast"<<endl; } };
heap_upcast 函数
// 堆得上溯操作 template<typename T> void Heap<T>::Heap_upcast(T *data,int i,const int length,bool (*pf)(T,T)) { if(data != NULL && length >= 0) { T max ; // have two children while( (i-1)/2 >= 0 ) { max = data[i]; //if(max <= data[(i-1)/2]) if(pf(max,data[(i-1)/2]) == false) break; // child bigger, and go up else { data[i] = data[(i-1)/2]; data[(i-1)/2] = max; i = (i-1)/2; } } } else { cout<<"exception of input Heap_downcast"<<endl; } };
相关文章推荐
- 百度面试题-求1到n个数的排序
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- 黑马程序员——Java基础——Map集合的学习
- 黑马程序员--基础整理02
- 黑马程序员——学习一维数组的地址
- 黑马程序员——String类和基本数据类型对象包装类
- 【黑马程序员】-----java基础----IO
- 身为java程序员你需要知道的网站(包含书籍,面试题,架构...)
- 黑马程序员_OC Foundation框架简介
- 黑马程序员——JAVA基础------集合框架(四)----Map集合
- 如何面试一个Android开发者
- 码农《二》
- Android面试
- 码农《一》
- 剑指offer-面试题17.合并两个排序的链表
- iOS程序员面试
- 关于PHP程序员解决问题的能力
- 黑马程序员--基础整理01
- 黑马程序员——JAVA基础------集合框架(三)----Iterator
- 【转】常见数字IC设计、FPGA工程师面试题