剑指Offer.40——最小的k个数

题目链接:https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/

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

输入:arr = [3,2,1], k = 2
输出:[1,2] 或者 [2,1]

输入:arr = [0,1,2,1], k = 1
输出:[0]

解题过程:

快速排序:

class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        if (k == 0 || k > arr.length) {
            return new int[0];
        }
        int[] res1 = quitSort(arr, 0, arr.length - 1);
        // System.out.println(Arrays.toString(res1));
        int[] res = new int[k];
        for (int i = 0; i < k; i++){
            res[i] = arr[i];
        }
        return res;
    }

    private int[] quitSort(int[] num, int low, int high){
        int pivatloc;
        if (low < high){
            pivatloc = partition(num, low, high);
            quitSort(num, low, pivatloc - 1);
            quitSort(num, pivatloc + 1, high);
        }
        return num;
    }

    private int partition(int[] num, int low, int high){
        int pivotkey = num[low];
        while (low < high){
            while(low < high && num[high] >= pivotkey){
                high--;
            }
            swap(num, low, high);

            while(low < high && num[low] <= pivotkey){
                low++;
            }
            swap(num, low, high);
        }
        return low;
    }

    private void swap(int[] arr, int a, int b){
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }
}

在这里插入图片描述

下面是带上java输入的代码:

import java.util.Arrays;
import java.util.Scanner;

class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++){
            arr[i] = in.nextInt();
        }
        in.nextLine();
        int k = in.nextInt();

        int[] res = solution.getLeastNumbers(arr, k);
        System.out.println(Arrays.toString(res));
        in.close();
    }

    public int[] getLeastNumbers(int[] arr, int k) {
        if (k == 0 || k > arr.length) {
            return new int[0];
        }
        int[] res1 = quitSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(res1));
        int[] res = new int[k];
        for (int i = 0; i < k; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    private int[] quitSort(int[] num, int low, int high) {
        int pivatloc;
        if (low < high) {
            pivatloc = partition(num, low, high);
            quitSort(num, low, pivatloc - 1);
            quitSort(num, pivatloc + 1, high);
        }
        return num;
    }

    private int partition(int[] num, int low, int high) {
        int pivotkey = num[low];
        while (low < high) {
            while (low < high && num[high] >= pivotkey) {
                high--;
            }
            swap(num, low, high);

            while (low < high && num[low] <= pivotkey) {
                low++;
            }
            swap(num, low, high);
        }
        return low;
    }

    private void swap(int[] arr, int a, int b) {
        int tmp = arr[a];
        arr[a] = arr[b];
        arr[b] = tmp;
    }
}

堆排序

使用了java的优先队列,由于java的优先队列是升序,所以要自己写一个比较器,实现降序排列。

class Solution {

    static Comparator<Integer> cmp = new Comparator<Integer>() {
      public int compare(Integer e1, Integer e2) {
        return e2 - e1;
      }
    };

    public int[] getLeastNumbers(int[] arr, int k) {
        int[] res = new int[k];
        if (k == 0 || k > arr.length) {
            return res;
        }
        Queue<Integer> queue = new PriorityQueue<>(cmp);
        for (int i = 0; i < k; i++) {
            queue.offer(arr[i]);
        }

        for (int i = k; i < arr.length; i++){
            if (arr[i] < queue.peek()){
                queue.poll();
                queue.offer(arr[i]);
            }
        }

        for (int i = 0; i < k; i++){
            res[i] = queue.peek();
            queue.poll();
        }
        return res;
    }
}

你可能感兴趣的:(LeetCode)