1. 重新排列数组
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] res = new int[2*n];
for (int i = 0; i < n; i++) {
res[2*i] = nums[i];
}
for (int j = 0; j < n; j++) {
res[2*j+1] = nums[j+n];
}
return res;
}
}
class Solution {
public int[] shuffle(int[] nums, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(nums[i]);
list.add(nums[i+n]);
}
for (int j = 0; j < 2*n; j++) {
nums[j] = list.get(j);
}
return nums;
}
}
2. 数组中的k个最强值
class Solution {
public int[] getStrongest(int[] arr, int k) {
Arrays.sort(arr);
int len = arr.length;
int left = 0;
int right = len - 1;
int mid = arr[(len-1)/2];
int[] res = new int[k];
int count = 0;
while (count < k) {
if (Math.abs(arr[right] - mid) >= Math.abs(arr[left] - mid)) {
res[count++] = arr[right];
right--;
} else {
res[count++] = arr[left];
left++;
}
}
return res;
}
}