https://leetcode.com/problems/merge-sorted-array/数组
Given two sorted integer arrays A and B, merge B into A as one sorted array.markdown
Note:spa
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 mand n respectively.code
题意:把两个有序数组合并成一个有序数组element
思路:逐个比较,小的复制给临时数组,最后把临时数组复制给Aleetcode
实现:get
public class Solution { public void merge(int A [], int m, int B[], int n) { int [] C = new int[ m + n]; for (int i = 0, j = 0, k = 0; k < m + n ; k ++) { if (i == m ) {// A数组用完,把B所有复制到C C[ k] = B[ j++]; continue ; } if (j == n ) {// B数组用完,把A所有复制到C C[ k] = A[ i++]; continue ; } if (A [i ] > B [j ])// 小的复制给C C[ k] = B[ j++]; else C[ k] = A[ i++]; } System. arraycopy( C, 0, A, 0, m + n); // 把C复制给A } }