输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。
思路分析:
法一:可以先数组排序一下,然后选取前K个数即可。
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
Arrays.sort(input);
ArrayList<Integer> re=new ArrayList<Integer>();
if(k==0)
return re;
if(k>0&&k<=input.length)
{
for(int i=0;i<k;i++){
re.add(input[i]);
}
}
return re;
}
}
法二:
采用堆排序的思想,先构建一个最大堆;然后,第k个元素开始分别于最大堆的最大值做比较,如果比最大值小,则进行替换调整;最后,输出前k个即为最小的k个数。
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> list=new ArrayList<Integer>();
if(k<1||k>input.length)
return list;
//构建最大堆
for(int len=k/2-1;len>=0;len--){
adjustMaxHeapSort(input,len,k-1);
}
//从第k个元素开始分别与最大堆的最大值做比较
//若比最大值小,则替换
int tmp;
for(int i=k;i<input.length;i++){
if(input[i]<input[0]){
tmp=input[0];
input[0]=input[i];
input[i]=tmp;
adjustMaxHeapSort(input,0,k-1);
}
}
for(int j=0;j<k;j++){
list.add(input[j]);
}
return list;
}
public void adjustMaxHeapSort(int[] input,int pos,int length){
int temp;
int child;
for(temp=input[pos];2*pos+1<=length;pos=child){
child=2*pos+1;
if(child<length&&input[child]<input[child+1]){
child++;
}
if(input[child]>temp){
input[pos]=input[child];
}
else{
break;
}
}
input[pos]=temp;
}
}