描述:合并两个排序的整数数组A和B变成一个新的数组
样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
1、Python:先将数组B加到数组A之后,然后对新数组进行排序
class Solution:
"""
@param A: sorted integer array A
@param B: sorted integer array B
@return: A new sorted integer array
"""
def mergeSortedArray(self, A, B):
# write your code here
C = A + B
C.sort()
return C
2、Java
public class Solution {
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
// write your code here
int Size_A = A.length;
int Size_B = B.length;
int[] result = new int[Size_A + Size_B];
int i = 0,j = 0,k = 0;
while(i < Size_A && j < Size_B){
if(A[i] <= B[j]){
result[k++] = A[i++];
}
else{
result[k++] = B[j++];
}
}
while(i < Size_A){
result[k++] = A[i++];
}
while(j < Size_B){
result[k++] = B[j++];
}
return result;
}
}