leetcode 88. Merge Sorted Array

題目:

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)


發佈了36 篇原創文章 · 獲贊 1 · 訪問量 9544
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章