Merge Sorted Array

题目描述
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]

分析:给定数组nums1,有效长度为m,给定数组nums2,有效长度为n;假设nums1的长度足够长,能够容纳下nums2中的数,请将nums2合并到nums1中,并返回。(结果依旧有序)

我的Code如下:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for(int i=0; i=j; x--){
                        nums1[x+1] = nums1[x];
                    }
                    nums1[j] = nums2[i];
                    break;
                }else{
                    if(j == m+i){
                        //说明nums1中数已经排序完了
                        nums1[j] = nums2[i];
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(Merge Sorted Array)