和leetcode1337核心部分相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   array: arr[] 

// a: after sorting; num: index
double[] a = new double[k];
int[] nums = new int[m];
for (int i = 0; i < m; ++i) {
nums[i] = i;
}
for (int i = 0; i < k; ++i) {
int index = i;
for (int j = i; j < m; ++j) {
if (arr[index] > arr[j]) {
index = j;
}
}
a[i] = arr[index];

double tmp1 = arr[i];
arr[i] = arr[index];
arr[index] = tmp1;

int tmp2 = nums[i];
nums[i] = nums[index];
nums[index] = tmp2;
}