题解 | #整型数组合并#

整型数组合并

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
nus2201602...:兄弟,你这个简历撕了丢了吧,就是一坨,去找几个项目,理解项目流程,看几遍就是你的了,看看八股就去干了,多看看牛客里别人发出来的简历,对着写,你这写的啥啊,纯一坨
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务