最小的K个数
来源:互联网 发布:黑帽seo如何赚钱 编辑:程序博客网 时间:2024/06/15 22:46
题目描述
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
快速排序
import java.util.*;public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> al=new ArrayList<Integer>(); if(input.length < k || k <= 0||input.length ==0) return al; int low=0; int high=input.length-1; int index=partition(input,low,high); while(index!=k-1){ if(index>k-1){ high=index-1; index=partition(input,low,high); } else{ low=index+1; index=partition(input,low,high); } } for(int i=0;i<k;i++){ al.add(input[i]); } return al; } public static int partition(int[] data,int low,int high){ int temp=data[low]; while(low<high){ while(temp<=data[high]&&low<high){ high--; } data[low]=data[high]; while(temp>=data[low]&&low<high){ low++; } data[high]=data[low]; } data[low]=temp; return low; }}
阅读全文
0 0
- 最小的K个数
- 最小的K个数
- 最小的K个数
- 最小的k个数
- 最小的k个数
- 最小的k个数
- 最小的k个数
- 最小的k个数
- 最小的k个数
- 最小的K个数
- 最小的k个数
- 最小的K个数
- 最小的K个数
- 最小的K个数
- 最小的K个数
- 最小的K个数
- 最小的k个数
- 最小的k个数
- HTML
- Nginx优化实战--参数优化
- java算法--水仙花数
- 使用boost库实现一个单例模式
- 异常
- 最小的K个数
- android webview详解
- web打印组件康虎云报表与odoo整合示例
- 关于多门编程语言的基本语句的展示(c++,后期再补充)
- Python网络编程--Echo服务
- RxJava2+Retrofit2精讲
- JavaScript实时校验用户输入
- 后缀数组 poj. 1743. Musial Theme
- Jmeter 跨线程组传递参数