Given sorted integer Arrays nums1 and nums2, merge nums2 into nums1 as one sorted Array.
Note:
Assume that nums1 have enough space (size that's greater or equal to m + n) to hold add Itional elements from nums2. The number of elements initialized in nums1and nums2 is m and n respectively.
Idea: This problem obviously merge the sort of shadow, just need to be flexible, from the back to the order of the forward. Place the large value behind the nums1.
The code is as follows:
public class Solution {public void merge (int[] nums1, int m, int[] nums2, int n) { /** * Merge sort idea, from back to forward * The big put in the last. Place first/ int len = m + n;//after sort total length while (M > 0 && n > 0) { if (Nums1[m-1] > Nums2[n-1]) {
nums1[--len] = nums1[--m]; } else{ Nums1[--len] = Nums2[--n]; } } The remaining digits while (M > 0) { Nums1[--len] = nums1[--m]; } while (n > 0) { Nums1[--len] = Nums2[--n];}} }
Leetcode 88.Merge Sorted Array (merge sorted array) thinking and method of solving problems