题解 | #整型数组合并#

整型数组合并

http://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int len1 = Integer.valueOf(scan.nextLine().trim());
        String[] numsStr1 = scan.nextLine().split(" ");
        int len2 = Integer.valueOf(scan.nextLine().trim());
        String[] numsStr2 = scan.nextLine().split(" ");
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < len1; i++) {
            hashSet.add(Integer.valueOf(numsStr1[i]));
        }
        for (int i = 0; i < len2; i++) {
            hashSet.add(Integer.valueOf(numsStr2[i]));
        }
        int[] nums = hashSet.stream().mapToInt(Integer::intValue).toArray();
        Arrays.sort(nums);
        // heapSort(nums);
        /// quickSort(nums);
        for (int num : nums) {
            System.out.print(num);
        }
    }
    public static void heapSort(int[] nums) {
        if (null == nums || nums.length < 2) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            heapInsert(nums, i);
        }
        int heapSize = nums.length;
        swap(nums, 0, --heapSize);
        while (heapSize > 0) {
            heapify(nums, 0, heapSize);
            swap(nums, 0, --heapSize);
        }
    }
    public static void heapInsert(int[] nums, int index) {
        while (nums[index] > nums[(index - 1) / 2]) {
            swap(nums, index, (index - 1) / 2);
            index = (index - 1) / 2;
        }
    }
    public static void heapify(int[] nums, int index, int heapSize) {
        int left = index * 2 + 1;
        while (left < heapSize) {
            int largest = left + 1 < heapSize && nums[left + 1] > nums[left] ? left + 1 : left;
            largest = nums[largest] > nums[index] ? largest : index;
            if (largest == index) {
                break;
            }
            swap(nums, index, largest);
            index = largest;
            left = index * 2 + 1;
        }
    }
    public static void swap(int[] nums, int p1, int p2) {
        int tmp = nums[p1];
        nums[p1] = nums[p2];
        nums[p2] = tmp;
    }
    public static void quickSort(int[] nums) {
        if (null == nums || nums.length < 2) {
            return;
        }
        process(nums, 0, nums.length - 1);
    }
    public static void process(int[] nums, int start, int end) {
        if (start >= end) {
            return;
        }
        int l = start - 1;
        int r = end + 1;
        int p = start;
        int val = nums[end];
        while (p < r) {
            if (nums[p] < val) {
                swap(nums, p++, ++l);
            } else if (nums[p] > val) {
                swap(nums, p, --r);
            } else {
                p++;
            }
        }
        process(nums, start, l);
        process(nums, r, end);
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 17:00

相关推荐

求个公司要我:接好运
点赞 评论 收藏
分享
我见java多妩媚:大外包
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务