当前位置:网站首页>Force buckle 88 Merge two ordered arrays
Force buckle 88 Merge two ordered arrays
2022-07-07 20:06:00 【Tomorrowave】
88. Merge two ordered arrays
Here are two buttons Non decreasing order Array of arranged integers nums1 and nums2, There are two other integers m and n , respectively nums1 and nums2 The number of elements in .
Would you please Merge nums2 To nums1 in , Make the merged array press Non decreasing order array .
Be careful : Final , The merged array should not be returned by the function , It's stored in an array nums1 in . In response to this situation ,nums1 The initial length of is m + n, The top m Elements represent the elements that should be merged , after n Elements are 0 , It should be ignored .nums2 The length of is n .
Example 1:
Input :nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output :[1,2,2,3,5,6]
explain : Need merger [1,2,3] and [2,5,6] .
The combined result is [1,2,2,3,5,6] , In which, bold italics indicates nums1 The elements in
Ideas
a = [1,2,3,4,7,5,6]
b = ['a','b']
c = ['h',12,'c']
a.extend(b)
a.extend(c)
print(a)
# result :[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())
# result :['1' '2' '3' 'a' 'b' 'c' 'h' '12' 'k']
a = [1,2,3,4] # The number of elements is different
b = ['a','b','c']
c = ['h',12,'k']
e = [a,b,c]
e = array(e)
print(e.flatten())
# result :[list([1, 2, 3, 4]) list(['a', 'b', 'c']) list(['h', 12, 'k'])]
The merging process of arrays
Code section
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
边栏推荐
猜你喜欢
Nunjuks template engine
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
使用高斯Redis实现二级索引
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Openeuler prize catching activities, to participate in?
数据孤岛是企业数字化转型遇到的第一道险关
Simulate the implementation of string class
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
9 atomic operation class 18 Rohan enhancement
【STL】vector
随机推荐
831. KMP字符串
R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
841. String hash
力扣599. 两个列表的最小索引总和
Openeuler prize catching activities, to participate in?
浏览积分设置的目的
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
力扣 1961. 检查字符串是否为数组前缀
Browse the purpose of point setting
gorilla官方:golang开websocket client的示例代码
力扣674. 最长连续递增序列
Tp6 realize Commission ranking
Make this crmeb single merchant wechat mall system popular, so easy to use!
微信公众号OAuth2.0授权登录并显示用户信息
Introduction to bit operation
CSDN syntax description
LeetCode_ 7_ five
R语言dplyr包mutate_at函数和min_rank函数计算dataframe中指定数据列的排序序号值、名次值、将最大值的rank值赋值为1
【Confluence】JVM内存调整