当前位置:网站首页>力扣 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
边栏推荐
- 力扣 1232.缀点成线
- torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
- A pot of stew, a collection of common commands of NPM and yarn cnpm
- PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
- 8 CAS
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
- Kubernetes——kubectl命令行工具用法详解
- UCloud是基础云计算服务提供商
- The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
- 现在股票开户可以直接在网上开吗?安全吗。
猜你喜欢
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
mock.js从对象数组中任选数据返回一个数组
Implement secondary index with Gaussian redis
mock. JS returns an array from the optional data in the object array
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
【STL】vector
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
Some important knowledge of MySQL
小试牛刀之NunJucks模板引擎
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
随机推荐
vulnhub之school 1
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
位运算介绍
torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
Detailed explanation of Flink parallelism and slot
线性基
RESTAPI 版本控制策略【eolink 翻译】
sql 常用优化
openEuler 有奖捉虫活动,来参与一下?
力扣 1232.缀点成线
IP 工具类
IP tools
关于自身的一些安排
The project manager's "eight interview questions" is equal to a meeting
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
毕业季|遗憾而又幸运的毕业季
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
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"