求数组中最小的k个数
题目:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
- package test;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.PriorityQueue;
- import org.junit.Test;
- public class GetLeastNumbers_Solution {
- /**
- * 基于优先队列,时间复杂度为o(nlogk)
- *
- * @param input
- * @param k
- * @return
- */
- public ArrayList<Integer> GetLeastNumbers_SolutionPriorityQuene(
- int[] input, int k) {
- ArrayList<Integer> result = new ArrayList<Integer>();
- if (input == null || input.length == 0 || k <= 0 || k > input.length)
- return result;
- PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(
- new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- return o2.compareTo(o1);
- }
- });
- for (int i = 0; i < k; i++) {
- maxHeap.offer(input[i]);
- }
- for (int j = k; j < input.length; j++) {
- if (input[j] < maxHeap.peek()) {
- maxHeap.poll();
- maxHeap.offer(input[j]);
- }
- }
- for (Integer integer : maxHeap) {
- result.add(integer);
- }
- return result;
- }
- /**
- * 基于堆排序,时间复杂度为o(nlogk)
- *
- * @param input
- * @param k
- * @return
- */
- public ArrayList<Integer> GetLeastNumbers_SolutionMaxHeap(int[] input, int k) {
- ArrayList<Integer> result = new ArrayList<Integer>();
- if (input == null || input.length == 0 || k <= 0 || k > input.length)
- return result;
- //构建最大堆
- builtMaxHeap(input,k-1);
- for (int i = k; i < input.length; i++) {
- //数组k位后的数字比堆顶小
- if (input[k] < input[0]) {
- input[0] = input[k];
- //调整堆
- builtMaxHeap(input, k - 1);
- }
- }
- for (int i = 0; i < k; i++) {
- result.add(input[i]);
- }
- return result;
- }
- /**
- * 构建、调整最大堆
- * @param a
- * @param lastIndex
- */
- public void builtMaxHeap(int[]a,int lastIndex){
- int parentIndex = ((lastIndex-1) >> 1);
- //从最后一个节点的父节点开始
- for(int i=parentIndex;i>=0;i--){
- //存在子节点
- while (i*2+1<=lastIndex){
- int leftIndex = i*2+1;
- int rightIndex = i*2+2;
- int biggerIndex = leftIndex;
- //存在右结点
- if (rightIndex <= lastIndex){
- if(a[rightIndex] > a[biggerIndex]){
- biggerIndex = rightIndex;
- }
- }
- //子节点中最大节点大于父节点
- if (a[biggerIndex] > a[i]){
- swap(a,i,biggerIndex);
- i = biggerIndex;
- }else{
- break;
- }
- }
- }
- }
- /**
- * 基于Partition函数,时间复杂度为o(n),原数组已被修改
- *
- * @param input
- * @param k
- * @return
- */
- public ArrayList<Integer> GetLeastNumbers_SolutionPartition(int[] input,
- int k) {
- ArrayList<Integer> result = new ArrayList<Integer>();
- if (input == null || input.length == 0 || k <= 0 || k > input.length)
- return result;
- int left = 0;
- int right = input.length - 1;
- int index = partition(input, 0, right);
- while (index != k - 1) {
- if (index > k - 1) {
- right = index - 1;
- index = partition(input, left, right);
- } else {
- left = index + 1;
- index = partition(input, left, right);
- }
- }
- for (int i = 0; i < k; i++) {
- result.add(input[i]);
- }
- return result;
- }
- /**
- * partition函数
- * @param a
- * @param left
- * @param right
- * @return
- */
- public int partition(int[] a, int left, int right) {
- while (left < right) {
- while (left < right && a[left] <= a[right]) {
- right--;
- }
- if (left < right) {
- swap(a, left, right);
- }
- while (left < right && a[left] <= a[right]) {
- left++;
- }
- if (left < right) {
- swap(a, left, right);
- }
- }
- return left;
- }
- public void swap(int[] a, int i, int j) {
- int tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- }
- @Test
- public void testGetLeastNumbers_Solution() {
- int[] a = { 4, 5, 1, 6, 2, 7, 3, 8 };
- int k = 4;
- ArrayList<Integer> list = GetLeastNumbers_SolutionPartition(a, k);
- System.out.println(list.toString());
- ArrayList<Integer> list2 = GetLeastNumbers_SolutionPriorityQuene(a, k);
- System.out.println(list2.toString());
- ArrayList<Integer> list3 = GetLeastNumbers_SolutionMaxHeap(a, k);
- System.out.println(list3.toString());
- }
- }
除了基于优先队列,时间复杂度为O(nlogk)、堆排序,时间复杂度为O(nlogk)、partition函数,时间复杂度为O(n)的解法之外,还有基于冒泡排序的解法时间复杂度为(nk)。
求数组中最小的k个数的更多相关文章
- 求一个数组中最小的K个数
方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...
- 【算法】数组与矩阵问题——找到无序数组中最小的k个数
/** * 找到无序数组中最小的k个数 时间复杂度O(Nlogk) * 过程: * 1.一直维护一个有k个数的大根堆,这个堆代表目前选出来的k个最小的数 * 在堆里的k个元素中堆顶的元素是最小的k个数 ...
- [算法]找到无序数组中最小的K个数
题目: 给定一个无序的整型数组arr,找到其中最小的k个数. 方法一: 将数组排序,排序后的数组的前k个数就是最小的k个数. 时间复杂度:O(nlogn) 方法二: 时间复杂度:O(nlogk) 维护 ...
- 《程序员代码面试指南》第八章 数组和矩阵问题 找到无序数组中最小的k 个数
题目 找到无序数组中最小的k 个数 java代码 package com.lizhouwei.chapter8; /** * @Description: 找到无序数组中最小的k 个数 * @Autho ...
- 小米笔试题:无序数组中最小的k个数
题目描述 链接:https://www.nowcoder.com/questionTerminal/ec2575fb877d41c9a33d9bab2694ba47?source=relative 来 ...
- 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...
- [剑指offer]数组中最小的K个数,C++实现
原创博文,转载请注明出处! http://github.com/wanglei5205 http://cnblogs.com/wanglei5205 # 题目 输入n个整数,找出其中最小的K个数.例如 ...
- 找到数组中最小的k个数
/*输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字, 则最小的4个数字是1.2.3.4. 示例 1: 输入:arr = [3,2,1], k = ...
- 面试题四十:数组中最小的k个数
方法一:先排序后寻找前k个数: 方法二:受面试题三十九,寻找超过一半的数的启发,只把里面的middle改成k-1就行: void HalfNum( int [ ] Array ,int k){ int ...
随机推荐
- 2018-03-03-解决win下凭据删除不干净而无法登录共项目录的问题
layout: post title: 2018-03-03-解决win下凭据删除不干净而无法登录共项目录的问题 key: 20180303 tags: GIT 版本管理 modify_date: 2 ...
- UVA - 1220 Party at Hali-Bula 树的最大独立集
题意: 给定n个人,存在上下级关系,每个人只有一个上级,求最大独立集.并判断最大独立集是否唯一 思路:d[i][0]表示以i为根的子树中,不选择第i个节点的最大独立集,f[i][0]表示以i为根的子 ...
- java 集合框架(十六)Map
一.概述 Map是一个包含键值对的集合,一个map不能有重复的键(key),而且每个键至多只能对应一个值.Map同Collection一样,它的所有通用实现都会提供一个转换器构造函数,接收一个Map类 ...
- 项目部署到Tomcat报错
1.今天晚上,我想把dojo项目部署到Tomcat中,结果发现部署不了,Tomcat报错.而且,这个错误白天时也碰到了. 错误具体详细如下: Publishing failed with multip ...
- Stanford Word Segmenter使用
1,下载 Stanford Word Segmenter软件包: Download Stanford Word Segmenter version 2014-06-16 2,在eclipse上建立一个 ...
- freemarker报错之一
freemarker 1.错误描述 java.io.FileNotFoundException: Template user.ftl not found. at freemarker.template ...
- 关于TS流的解析
字节.在TS流里可以填入很多类型的数据,如视频.音频.自定义信息等.他的包的结构为,包头为4个字节,负载为184个字节(这184个字节不一定都是有效数据,有一些可能为填充数据). 工作形式: 因为在T ...
- Android中selector的使用
第一种方法(强烈推荐) 方法:selector做遮罩,原图做background. 我们做按钮的时候经常需要用两个图片来实现按钮点击和普通状态的样式,这就需要提供两种图片,而且每个分辨率下还有多套图片 ...
- 【网络流24题22】最长k可重线段集问题
题面戳我 sol 千万!千万!不要理解错题意了!最长K可重,不是说线段最多K可重!你以为计算几何? 原文:使得在\(x\)轴上的任何一点\(p\),\(S\)中与直线\(x=p\)相交的开线段个数不超 ...
- [BZOJ2326] [HNOI2011] 数学作业 (矩阵乘法)
Description Input Output Sample Input Sample Output HINT Source Solution 递推式长这样:$f[n]=f[n-1]*10^k+n$ ...