当前位置:网站首页>力扣 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
边栏推荐
- 【Confluence】JVM内存调整
- R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
- 剑指 Offer II 013. 二维子矩阵的和
- equals 方法
- Kirin Xin'an cloud platform is newly upgraded!
- vulnhub之Funfox2
- 浏览积分设置的目的
- Simulate the implementation of string class
- Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
- 力扣 2315.统计星号
猜你喜欢

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

位运算介绍

Openeuler prize catching activities, to participate in?
让这个 CRMEB 单商户微信商城系统火起来,太好用了!

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

干货分享|DevExpress v22.1原版帮助文档下载集合

开源OA开发平台:合同管理使用手册

Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记

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

vulnhub之Funfox2
随机推荐
编译器优化那些事儿(4):归纳变量
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
[RT thread env tool installation]
力扣 459. 重复的子字符串
Cloud 组件发展升级
9 atomic operation class 18 Rohan enhancement
关于自身的一些安排
Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
注解。。。
毕业季|遗憾而又幸运的毕业季
Some important knowledge of MySQL
IP tools
CSDN语法说明
JVM 类加载机制
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
Matplotlib drawing 3D graphics
Mysql, sqlserver Oracle database connection mode
The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
Introduction to bit operation
小试牛刀之NunJucks模板引擎