当前位置:网站首页>力扣 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
边栏推荐
- How to buy stocks on your mobile phone and open an account? Is it safe to open an account
- ASP.NET幼儿园连锁管理系统源码
- Time tools
- What does "true" mean
- The project manager's "eight interview questions" is equal to a meeting
- 凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
- AD域组策略管理
- JVM 类加载机制
- el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
- LeetCode_ 7_ five
猜你喜欢
华南X99平台打鸡血教程
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
Implement secondary index with Gaussian redis
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
转置卷积理论解释(输入输出大小分析)
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
Cloud 组件发展升级
mysql 的一些重要知识
Nunjuks template engine
随机推荐
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
Boot 和 Cloud 的版本选型
Time tools
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
JVM GC垃圾回收简述
ASP.NET幼儿园连锁管理系统源码
vulnhub之tre1
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
Kirin Xin'an cloud platform is newly upgraded!
mock. JS returns an array from the optional data in the object array
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
Sword finger offer II 013 Sum of two-dimensional submatrix
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
时间工具类
力扣 2315.统计星号
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
Matplotlib drawing 3D graphics