LeetCode - Merge Sorted Array
Description:
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
两个有序数组和成一个数组。
public class Solution { public void merge(int A[], int m, int B[], int n) { int[] storage = new int[m+n]; for(int i=0,j=0,cnt=0; cnt<m+n; i++,j++) { if(i<m) { storage[cnt++] = A[i]; } if(j<n) { storage[cnt++] = B[j]; } } Arrays.sort(storage); int j = 0; for(int i : storage) { A[j++] = i; } } }
作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。