Created
March 10, 2017 02:33
-
-
Save mindthink/f1fda4c946de3446986282c3c9abb1f0 to your computer and use it in GitHub Desktop.
leetcode 88
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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