標籤:

真的智障,真寫不出來,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.

class Solution(object):n def merge(self, nums1, m, nums2, n):n """n :type nums1: List[int]n :type m: intn :type nums2: List[int]n :type n: intn :rtype: void Do not return anything, modify nums1 in-place instead.n """n # i = m + n - 1n # j = m - 1n # k = n - 1n # while i >= 0:n # if j >= 0 and k >= 0:n # if nums1[j] < nums2[k]:n # nums1[i] = nums2[k]n # else:n # nums1[i] = nums1[j]n # elif j >= 0:n # nums1[i] = nums1[j]n # j -= 1n # elif k >= 0:n # nums1[i] = nums1[k]n # k -= 1n # i -= 1n while n > 0:n if m <= 0 or nums1[m-1] < nums2[n-1]:n nums1[m+n-1] = nums2[n-1]n n -= 1n else:n nums1[m+n-1] = nums1[m-1]n m -= 1n

為什麼會出現

Input:[0]

0

[1]

1

這種testcase啊?難道m,n的值不應該都是列表的長度嗎?。。。哪位大神能解釋下?


推薦閱讀:

LeetCode 680. Valid Palindrome II
leetcode-2
Leetcode-38-Count-and-Say(學渣的痛大神們不懂。。。)

TAG:LeetCode |