问题描述:

输入n个整数,找出其中最小的k个数

思路1:

先排序,再取前k个

时间复杂度O(nlogn)

下面给出快排序的代码(基于下面Partition函数的方法)

public void QuickSort(int[] arr, int start, int end){

    if(start == end){
return;
}
int index = Partition(arr, start, end); if(index > start){
QuickSort(arr, start, index - 1);
}
if(index < end){
QuickSort(arr, index + 1, end);
}
}

相应的求最小的k个数的代码:

static boolean InvalidInput = false;
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> result = new ArrayList<Integer>();
if(input == null || input.length == 0 || k > input.length || k <= 0){
InvalidInput = true;
return result;
} int start = 0;
int end = input.length - 1;
QuickSort(input, start, end); for(int i = 0; i < k; i++){
result.add(input[i]);
} return result;
}

思路2:(适合海量数据的输入)

—维护一个大小为k的容器。

—每次从输入整数读取一个数,如果容器中的数字少于k个,则直接加入,否则,找出容器中的最大值,与当前数字比较,若大于最大值,则不管,若小于,则替换最大值。

—时间复杂度O(n*log*k)

关于容器的数据结构的选择:

  1. 数组:查找最大值需要O(k)
  2. 最大堆:查找最大值需要O(1),但需要O(logk)时间完成删除及插入操作。
  3. 红黑树

思路3:基于Partition,会改变原始数组

  • 首先来看看Partition的原理与具体实现以及结果

Partition:思想是随机选择一个数,调整数组使得比所选数小的数字移动到数组的左边,比选择的数字大的数字大的数移到数组的右边。上一文中也用到了Partition的思想,但是并没有弄懂。下面对代码进行了详细的注释。

基于上述Partition函数的解析,我们能用它来实现本文中的求最小的k个数

思想:如果基于数组的第k个数字来调整,使得比第k个数字小的所有数字都位于数组的左边,比第k个数字大的数字都位于数组的右边。这样调整后,位于数组中左边的k个数字就是最小的k个数字。

缺陷:会改变原始数组;所得到的k个最小的数字不是有序的。

代码:

import java.util.ArrayList;

public class Solution {
static boolean InvalidInput = false;
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> result = new ArrayList<Integer>();
if(input == null || input.length == 0 || k > input.length || k <= 0){
InvalidInput = true;
return result;
} int start = 0;
int end = input.length - 1;
int index = Partition(input, start ,end); while(index != k-1){
if(index > k - 1){
end = index - 1;
index = Partition(input, start ,end);
}else{
start = index + 1;
index = Partition(input, start, end);
}
} for(int i = 0; i < k; i++){
result.add(input[i]);
} return result;
} public int Partition(int[] arr, int start, int end){
if(arr == null || arr.length == 0 || start < 0 || end < 0){
return -1;
} int index = start + (int)(Math.random() * ((end - start) + 1));//随机选择一个作为标杆的数字 //将标杆放在数组最后一位
int tmp = arr[index]; arr[index] = arr[end]; arr[end] = tmp; int small = start - 1;//small用来存储从右到左第一个小于标杆的数字的下标
for(index = start; index < end; index++){
if(arr[index] < arr[end]){//如果小于标杆
small++;//更新第一个小的
if(small != index){//如果当前遍历的不是第一个小的
tmp = arr[index];arr[index] = arr[small];arr[small] = tmp;//将当前遍历的数字放在第一个小的位置上 }
}
} //由于small指示的是从右到左第一个小于标杆的,而此时标杆还放在数组最后,因此,应该将标杆放在small后面一位。
small++;
tmp = arr[small];arr[small] = arr[end]; arr[end] = tmp; return small;//返回位置为所选择的标杆最后的位置 }
}

剑指Offer:面试题30——最小的k个数(java实现)的更多相关文章

  1. 剑指offer 面试题40. 最小的k个数

    O(N)划分法,注意这个方法会改变原数据(函数参数是引用的情况下)!当然也可以再定义一个新容器对其划分 要求前k小的数,只要执行快排划分,每次划分都会把数据分成大小两拨.直到某一次划分的中心点正好在k ...

  2. 剑指Offer - 九度1371 - 最小的K个数

    剑指Offer - 九度1371 - 最小的K个数2013-11-23 15:45 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是 ...

  3. 剑指offer(29)最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 题目分析 这题有两种方法来做. 第一种就是基于partition的 ...

  4. 【剑指Offer】29、最小的K个数

      题目描述:   输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4.   解题思路:   本题最直观的解法就是将输入的n个整数排 ...

  5. 剑指offer面试题30:最小的k个数

    一.题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.解题思路 1.思路1 首先对数组进行排序,然后取出前k个数 ...

  6. 剑指offer面试题5 从头到尾打印链表(java)

    注:(1)这里体现了java数据结构与C语言的不同之处 (2)栈的操作直接利用stack进行 package com.xsf.SordForOffer; import java.util.Stack; ...

  7. 面试题30.最小的k个数

    题目:输入n个整数,找出其中最小的k个数,例如输入4,5,1,6,2,7,3,8 这8个数字,则最小的四个数字为1,2,3,4, 这道题是典型的TopK问题,剑指Offer提供了两种方法来实现,一种方 ...

  8. 剑指offer——面试题30:包含min函数的栈

    #include"iostream" #include"stdio.h" using namespace std; ; ; template<typena ...

  9. 剑指offer面试题30.包含min函数的栈

    一开始写的垃圾代码,push和pop都是O(N) class Solution { public: vector<int> vec; int min_val=INT_MAX,min_cnt ...

随机推荐

  1. split,slice,splice,replace的用法

    split()方法用于把一个字符串分割成字符串数组 str.split("字符串/正则表达式从该参数制定额地方分割str",可选,可指定返回数组的最大长度,如果没设置参数,整个字符 ...

  2. linux的压缩命令

    gzip: Linux压缩保留源文件的方法: gzip –c filename > filename.gz Linux解压缩保留源文件的方法: gunzip –c filename.gz > ...

  3. 【Cocos2d-x 3.x】内存管理机制与源码分析

    侯捷先生说过这么一句话 :  源码之前,了无秘密. 要了解Cocos2d-x的内存管理机制,就得阅读源码. 接触Cocos2d-x时, Cocos2d-x的最新版本已经到了3.2的时代,在学习Coco ...

  4. uva 10820

    /* 交表 _________________________________________________________________________________ #include < ...

  5. Linear Algebra lecture10 note

    Four fundamental subspaces( for matrix A)   if A is m by n matrix: Column space  C(A) in Rm (列空间在m维实 ...

  6. HackerRank-Longest Subarray

    give an array and target value, find the max length of the subarray which sum of the elements is les ...

  7. VS2010遇到fatal error C1083: 无法打开预编译头文件:“xxx.pch”: No such file or directory

    对C++和VS2010非常不熟悉,但是无奈赶着项目,只能看了点基础就上手,然后就碰到这个问题了. 原因分析: http://bbs.csdn.net/topics/340191697?page=1 编 ...

  8. Lua 调用 Opencv 的方法

    Lua 调用 Opencv 的方法 最近想用 Lua 调用 Opencv 进行相关像素级操作,如:bitwise_and 或者 bitwise_or,从而完成图像 IoU 的计算. 那么,怎么用 Lu ...

  9. 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层

    View层: $('#btnNews').click(function() { if (!confirm('确定要提交吗?')) { return; } var frontViewData = []; ...

  10. sokite

    <?php interface Proto { //连接 function conn($url); //发送get请求 function get(); //发送post请求 function p ...