NC119 最小的K个数

描述

给定一个长度为 n 的可能有重复值的数组,找出其中不去重的最小的 k 个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4(任意顺序皆可)。
数据范围:0\le k,n \le 100000≤k,n≤10000,数组中每个数的大小0 \le val \le 10000≤val≤1000
要求:空间复杂度 O(n)O(n) ,时间复杂度 O(nlogn)O(nlogn)

示例1

输入:[4,5,1,6,2,7,3,8],4
返回值:[1,2,3,4]
说明:
返回最小的4个数即可,返回[1,3,2,4]也可以

示例2

输入:[1],0
返回值:[]

示例3

输入:[0,1,2,1,2],3
返回值:[0,1,1]


解题

1.先排序,再将前K个数组存起来返回

  • 可以使用快排序哦
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = new int[]{0, 1, 1, 1, 4, 5, 3, 7, 7, 8, 10, 2, 7, 8, 0, 5, 2, 16, 12, 1, 19, 15, 5, 18, 2, 2, 22, 15, 8, 22, 17, 6, 22, 6, 22, 26, 32, 8, 10, 11, 2, 26, 9, 12, 9, 7, 28, 33, 20, 7, 2, 17, 44, 3, 52, 27, 2, 23, 19, 56, 56, 58, 36, 31, 1, 19, 19, 6, 65, 49, 27, 63, 29, 1, 69, 47, 56, 61, 40, 43, 10, 71, 60, 66, 42, 44, 10, 12, 83, 69, 73, 2, 65, 93, 92, 47, 35, 39, 13, 75};
        solution.GetLeastNumbers_Solution(arr,20);
        solution.quickSort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
        ArrayList<Integer> res = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        if (k == 0 || input.length == 0) {
            return res;
        }
        quickSort(input, 0, input.length - 1);
        int index = 0;
        while (k != 0) {
            res.add(input[index]);
            index++;
            k--;
        }
        return res;
    }

    private void quickSort(int[] arr, int start, int end) {
        if (arr.length == 0 || start < 0 || end >= arr.length || start > end) {
            return;
        }
        int smallIndex = partition(arr, start, end);
        if (smallIndex > start) {
            quickSort(arr, start, smallIndex - 1);
        }
        if (smallIndex < end) {
            quickSort(arr, smallIndex + 1, end);
        }

    }

    private int partition(int[] arr, int start, int end) {
        int p = arr[start];
        while (start < end) {
            while (start < end && arr[end] > p) {
                end--;
            }
            swap(arr, start, end);
            while (start < end && arr[start] <= p) {
                start++;
            }
            swap(arr, start, end);
        }
        return start;
    }

    private void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

在这里插入图片描述

  • 也可以使用排序的API
public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
        ArrayList<Integer> res = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        if (k == 0 || input.length == 0) {
            return res;
        }
        Arrays.sort(input);
        int index = 0;
        while (k != 0) {
            res.add(input[index]);
            index++;
            k--;
        }
        return res;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值