Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
要求將兩個有序的排序合併成一個有序的數組
思路:將數組nums1和nums2同時從後往前掃,每次迭代中nums1和nums2中較大的一個元素加入結果數組中,然後對應的數組索引減1,另外一個數組不動。爲了防止覆蓋nums1的前面元素,結果數組也從後往前掃。
代碼AC:
時間複雜度爲O(m+n)