88 Merge Sorted Array
https://leetcode.com/problems/merge-sorted-array/#/description
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.
Note:
You may assume thatnums1has enough space (size that is greater or equal tom+n) to hold additional elements fromnums2. The number of elements initialized innums1andnums2aremandnrespectively.
分析及代码
public void merge(int[] nums1, int m, int[] nums2, int n) {
int idx1 = m - 1;
int idx2 = n - 1;
int len = m + n - 1;
while(idx1 >= 0 && idx2 >= 0) {
nums1[len--] = nums1[idx1] > nums2[idx2]? nums1[idx1--]: nums2[idx2--];
}
while(idx2 >= 0) {
nums1[len--] = nums2[idx2--];
}
}