auto commit
This commit is contained in:
parent
bd8b6bfb14
commit
dbf64f2e02
|
@ -1779,39 +1779,42 @@ public int MoreThanHalfNum_Solution(int[] nums) {
|
|||
找到第 K 个元素之后,就可以再遍历一次数组,所有小于等于该元素的数组元素都是最小的 K 个数。
|
||||
|
||||
```java
|
||||
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
|
||||
ArrayList<Integer> res = new ArrayList<>();
|
||||
if (k > input.length || k <= 0) return res;
|
||||
findKthSmallest(input, k - 1);
|
||||
for (int i = 0; i < k; i++) {
|
||||
res.add(input[i]);
|
||||
}
|
||||
return res;
|
||||
public ArrayList<Integer> GetLeastNumbers_Solution(int[] nums, int k) {
|
||||
ArrayList<Integer> ret = new ArrayList<>();
|
||||
if (k > nums.length || k <= 0)
|
||||
return ret;
|
||||
int kthSmallest = findKthSmallest(nums, k - 1);
|
||||
// findKthSmallest 会让改变数组,使得前 k 个数都是最小的 k 个数
|
||||
for (int i = 0; i < k; i++)
|
||||
ret.add(nums[i]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void findKthSmallest(int[] nums, int k) {
|
||||
public int findKthSmallest(int[] nums, int k) {
|
||||
int l = 0, h = nums.length - 1;
|
||||
while (l < h) {
|
||||
int j = paration(nums, l, h);
|
||||
if (j == k) break;
|
||||
else if (j > k) h = j - 1;
|
||||
else l = j + 1;
|
||||
int j = partition(nums, l, h);
|
||||
if (j == k)
|
||||
break;
|
||||
if (j > k)
|
||||
h = j - 1;
|
||||
else
|
||||
l = j + 1;
|
||||
}
|
||||
return nums[k];
|
||||
}
|
||||
|
||||
private int paration(int[] nums, int l, int h) {
|
||||
//nums[l+1,i] nums[j,h]
|
||||
private int partition(int[] nums, int l, int h) {
|
||||
int i = l, j = h + 1;
|
||||
while (true) {
|
||||
while (i < h && nums[++i] < nums[l]) ;
|
||||
while (j > l + 1 && nums[--j] > nums[l]) ;
|
||||
if (i >= j) break;
|
||||
while (j > l && nums[l] < nums[--j]) ;
|
||||
if (i >= j)
|
||||
break;
|
||||
swap(nums, i, j);
|
||||
}
|
||||
swap(nums, l, j);
|
||||
return j;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void swap(int[] nums, int i, int j) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user