1、题目描述:
对n 个有序数组,合并成为一个有序数组
输入为:
4
2 4 8
3 5 6
5 8 9 11
1 6 7 10 12
输出为:
1 2 3 4 4 5 6 6 7 8 8 9 10 11 12
2、题解:
排序
n = int(input())
nums = []
for j in range(n):
nums.append([int(i) for i in input().split()])
# print(nums)
def solution(n,nums):
def hebing(nums1,nums2):
res = [0] * (len(nums1) + len(nums2))
left,right = 0,0
p = 0
while left < len(nums1) and right < len(nums2):
if nums1[left] < nums2[right]:
res[p] = nums1[left]
left += 1
else:
res[p] = nums2[right]
right += 1
p += 1
# print(res)
if left < len(nums1):
res[left + right:] = nums1[left:]
if right < len(nums2):
res[left + right:] = nums2[right:]
# res += nums1[left:] if len(nums1) > left else nums2[right:]
return res
if n == 1:
return nums
res = hebing(nums[0],nums[1])
for i in range(2,n):
# temp = hebing(nums[i],res)
res = hebing(nums[i],res)
# print(res)
return res
print(solution(n,nums))