1. 문제
2. 나의 풀이
2-1. 앞에서부터 비교해서 채우기
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
if m < 0 and n < 0:
return
if n < 0:
return
n1_index = 0
for i in range(len(nums1)-m):
nums1[len(nums1)-1-i] = -10000000000
for n2 in nums2:
while nums1[n1_index] > -10000000000 and nums1[n1_index] <= n2:
if n1_index + 1 >= len(nums1):
break
n1_index += 1
temp = nums1[n1_index:-1]
nums1[n1_index] = n2
nums1[n1_index+1:] = temp
2-2. 뒤에서 비교해서 채우기
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
if m < 0 and n < 0:
return
if n < 0:
return
l = m + n
while (m > 0 and n > 0):
mValue = nums1[m-1]
nValue = nums2[n-1]
if nValue > mValue:
nums1[l-1] = nValue
n -= 1
else:
nums1[l-1] = mValue
m -= 1
l -= 1
for i in range(0,n):
nums1[i] = nums2[i]
3. 남의 풀이