题解 | #整型数组合并#

整型数组合并

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;
    }
}

全部评论

相关推荐

10-07 20:48
门头沟学院 Java
听说改名就会有offer:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务