Skip to content

Instantly share code, notes, and snippets.

@mindthink mindthink/88.py
Created Mar 10, 2017

Embed
What would you like to do?
leetcode 88
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: void Do not return anything, modify nums1 in-place instead.
"""
i = m - 1
j = n - 1
k = m + n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[k] = nums1[i]
k -= 1
i -= 1
else:
nums1[k] = nums2[j]
k -= 1
j -= 1
while j >= 0:
nums1[k] = nums2[j]
k -= 1
j -= 1
if __name__ == '__main__':
sol = Solution()
nums1 = [0, 1, 4, 5, 6, 0, 0, 0, 0]
nums2 = [2, 3, 6, 7]
sol.merge(nums1, 5, nums2, 4)
print(nums1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.