最小的K个数
题目
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
解题思路
- 利用堆排序原理,计算出最小的 k 个数
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
ArrayList<Integer> res = new ArrayList<>();
if (k > input.length || k == 0) {
return res;
}
for (int i = input.length - 1; i >= 0; i--) {
minHeap(input, 0, i);
swap(input, 0, i);
res.add(input[i]);
if (res.size() == k) break;
}
return res;
}
private void minHeap(int[] heap, int start, int end) {
if (start == end) {
return;
}
int childLeft = start * 2 + 1;
int childRight = childLeft + 1;
if (childLeft <= end) {
minHeap(heap, childLeft, end);
if (heap[childLeft] < heap[start]) {
swap(heap, start, childLeft);
}
}
if (childRight <= end) {
minHeap(heap, childRight, end);
if (heap[childRight] < heap[start]) {
swap(heap, start, childRight);
}
}
}
private void swap(int[] nums, int a, int b) {
int t = nums[a];
nums[a] = nums[b];
nums[b] = t;
}
当前内容版权归 Hayden Yang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Hayden Yang .