当前位置:网站首页>力扣 88.合并两个有序数组
力扣 88.合并两个有序数组
2022-07-07 17:53:00 【Tomorrowave】
88.合并两个有序数组
给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。
注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
示例 1:
输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
解释:需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素
思路
a = [1,2,3,4,7,5,6]
b = ['a','b']
c = ['h',12,'c']
a.extend(b)
a.extend(c)
print(a)
#结果:[1, 2, 3, 4, 7, 5, 6, 'a', 'b', 'h', 12, 'c']
from numpy import array
a = [1,2,3]
b = ['a','b','c']
c = ['h',12,'k']
e = [a,b,c]
e = array(e)
print(e.flatten())
#结果:['1' '2' '3' 'a' 'b' 'c' 'h' '12' 'k']
a = [1,2,3,4] #元素个数不同
b = ['a','b','c']
c = ['h',12,'k']
e = [a,b,c]
e = array(e)
print(e.flatten())
#结果:[list([1, 2, 3, 4]) list(['a', 'b', 'c']) list(['h', 12, 'k'])]
数组的合并过程
代码部分
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
""" Do not return anything, modify nums1 in-place instead. """
nums1[m:] = nums2
nums1.sort()
return nums1
边栏推荐
猜你喜欢

模拟实现string类

Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
Make this crmeb single merchant wechat mall system popular, so easy to use!

论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》

mock.js从对象数组中任选数据返回一个数组

# 欢迎使用Markdown编辑器

Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )

LeetCode_ 7_ five

vulnhub之Funfox2

一张图深入的理解FP/FN/Precision/Recall
随机推荐
9 atomic operation class 18 Rohan enhancement
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
Kirin Xin'an joins Ningxia commercial cipher Association
mysql 的一些重要知识
一锅乱炖,npm、yarn cnpm常用命令合集
Introduction to bit operation
MySQL、sqlserver oracle数据库连接方式
2022如何评估与选择低代码开发平台?
IP tools
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
ant desgin 多选
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
Welcome to the markdown editor
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
位运算介绍
YoloV6:YoloV6+Win10---训练自己得数据集
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Notes...
Kirin Xin'an cloud platform is newly upgraded!