剑指 Offer - 29:最小的k个数

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4

题目链接:https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf

解题思路

1、快排 partition 思想
2、大根堆思想

import java.util.*;

public class Solution {
    // 快排 partition 思想
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list = new ArrayList<>();
        if(input == null || input.length == 0 || k <= 0 || k > input.length){
             return list;
         }
        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++) {
            list.add(input[i]);
        }
        return list;
    }
    
    private int partition(int[] arr, int left, int right) {
        if(arr == null || arr.length == 0 || left < 0 || right >= arr.length){
             return -1;
         }
        int pivot = arr[left];
        while (left < right) {
            while (arr[right] >= pivot && left < right) right--;
            swap(arr, left, right);
            while (arr[left] <= pivot && left < right) left++;
            swap(arr, left, right);
        }
        return left;
    }
    
    private void swap(int[] arr, int m, int n) {
        int temp = arr[m];
        arr[m] = arr[n];
        arr[n] = temp;
    }
}
import java.util.*;

public class Solution {
    // 大根堆思想
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list = new ArrayList<>();
        if(input == null || input.length == 0 || k <= 0 || k > input.length){
             return list;
        }
        int[] kArray = Arrays.copyOfRange(input, 0, k);
        // 创建大根堆
        buildHeap(kArray);
        
        for (int i = k; i < input.length; i++) {
            if (input[i] < kArray[0]) {
                kArray[0] = input[i];
                maxHeap(kArray, 0);
            }
        }
        
        for (int i = 0; i < k; i++) {
            list.add(kArray[i]);
        }
        return list;
    }
    
    private void buildHeap(int[] input) {
        for (int i = input.length / 2 - 1; i >= 0; i--) {
            maxHeap(input, i);
        }
    }
    
    private void maxHeap(int[] input, int i) {
        int left = 2 * i + 1;
        int right = left + 1;
        int max = i;
        if (left < input.length && input[left] > input[max]) {
            max = left;
        }
        if (right < input.length && input[right] > input[max]) {
            max = right;
        }
        if (max != i) {
            swap(input, i, max);
            maxHeap(input, max);
        }
    }
    
    private void swap(int[] input, int i, int j) {
        int temp = input[i];
        input[i] = input[j];
        input[j] = temp;
    }
    
}

你可能感兴趣的:(时间效率,Java,剑指Offer,Java,算法,剑指Offer)