leetcode:Merge Sorted ArrayII

1、

 Merge two given sorted integer array A and B into a new sorted integer array.

A=[1,2,3,4]

B=[2,4,5,6]

return [1,2,2,3,4,4,5,6]

2、思路

  1、创建一个大集合

  2、判断,谁小谁填入

3、

  

public static int[] mergeSortedArray(int[] A, int[] B) {
        int i = 0, j = 0;
        int[] sum = new int[A.length + B.length];
        int num = 0;
//双方都有值判断
        while (i < A.length && j < B.length) {

            if (A[i] > B[j]) {
                sum[num++] = B[j++];
            } else {
                sum[num++] = A[i++];
            }
        }
        while (i < A.length) {
            sum[num++] = A[i++];
        }
        while (j < B.length) {
            sum[num++] = B[j++];
        }
        return sum;
    }
工作小总结,有错请指出,谢谢。
原文地址:https://www.cnblogs.com/zilanghuo/p/5329382.html