题解 | #整型数组合并#
整型数组合并
https://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n1 = Integer.parseInt(br.readLine());
String[] strs1 = br.readLine().split(" ");
int n2 = Integer.parseInt(br.readLine());
String[] strs2 = br.readLine().split(" ");
Set<Integer> set = new HashSet<>();
for (String str : strs1) {
if (!set.contains(Integer.parseInt(str))) {
set.add(Integer.parseInt(str));
}
}
for (String str : strs2) {
if (!set.contains(Integer.parseInt(str))) {
set.add(Integer.parseInt(str));
}
}
int[] nums = new int[set.size()];
int index = 0;
for (int num:set){
nums[index] = num;
index++;
}
quickSort(nums,0,nums.length-1);
for (int i=0;i<nums.length;i++){
System.out.print(nums[i]);
}
}
public static void quickSort(int[] nums, int startIndex, int endIndex) {
if (startIndex >= endIndex) {
return;
}
int p = partition(nums, startIndex, endIndex);
quickSort(nums, startIndex, p - 1);
quickSort(nums, p + 1, endIndex);
}
public static int partition(int[] nums, int startIndex, int endIndex) {
int pivot = nums[startIndex];
int left = startIndex;
int right = endIndex;
while (left < right) {
while (left < right && nums[right] >= pivot) {
right--;
}
while (left < right && nums[left] <= pivot) {
left++;
}
if (left < right) {
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
}
}
nums[startIndex] = nums[left];
nums[left] = pivot;
return left;
}
}
查看6道真题和解析