https://leetcode.com/problems/merge-sorted-array/
您得到了两个排序数组及其长度,您需要将它们组合成一个排序数组。
示例1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
示例2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1]
说明:我们正在合并的数组是[1]和[]。的结果合并为[1]。
示例3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1 Output: [1]
我的代码:
class Solution:
def merge(self,a, m, b, n):
i,j = 0,0
arr=[]
while i<m and j<n:
if a[i]<b[j]:
arr.append(a[i])
i+=1
else:
arr.append(b[j])
j+=1
if m!=i:
while i!=m:
arr.append(a[i])
i+=1
if n!=j:
while j!=n:
arr.append(b[j])
j+=1
return arr
问题是我无法获得正确的输出,代码在ide jupyter或vs代码中运行良好,但在leetcode上不工作。
仔细查看链接中的需求:
"""
Do not return anything, modify nums1 in-place instead.
"""
你的任务是修改a
,而不是返回任何