File tree Expand file tree Collapse file tree 1 file changed +11
-12
lines changed
Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Original file line number Diff line number Diff line change @@ -204,21 +204,20 @@ class Solution:
204204 """
205205 Do not return anything, modify nums1 in-place instead.
206206 """
207- # 整体思路相似,只不过没有使用 current 指针记录当前填补位置
207+ pos = m + n - 1
208208 while m > 0 and n > 0 :
209- if nums1[m- 1 ] <= nums2[n- 1 ]:
210- nums1[m + n - 1 ] = nums2[n- 1 ]
209+ if nums1[m - 1 ] < nums2[n - 1 ]:
210+ nums1[pos ] = nums2[n - 1 ]
211211 n -= 1
212212 else :
213- nums1[m+ n- 1 ] = nums1[m- 1 ]
214- m -= 1
215- """
216- 由于没有使用 current,第一步比较结束后有两种情况:
217- 1. 指针 m>0,n=0,此时不需要做任何处理
218- 2. 指针 n>0,m=0,此时需要将 nums2 指针左侧元素全部拷贝到 nums1 的前 n 位
219- """
220- if n > 0 :
221- nums1[:n] = nums2[:n]
213+ nums1[pos] = nums1[m - 1 ]
214+ m -= 1
215+ pos -= 1
216+ while n > 0 :
217+ nums1[pos] = nums2[n - 1 ]
218+ n -= 1
219+ pos -= 1
220+
222221```
223222
224223** 复杂度分析**
You can’t perform that action at this time.
0 commit comments