当前位置:网站首页>2. merge two ordered arrays
2. merge two ordered arrays
2022-06-26 10:09:00 【later_ rql】
2. Merge two ordered arrays
Title Description
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 .
Their thinking
Solution 1 : Double finger needling
Ideas :
Time complexity :O(n)
Spatial complexity :O(m+n)=O(n)
Solution 2 : Direct merge sequence method
Ideas : Merge the two arrays directly , Then sort the merged array
Time complexity : On average O((m+n)log(m+n))
Spatial complexity : On average O(log(m+n))
Solution 3 : Reverse double pointer method
Ideas : Traverse and compare the two arrays from back to front , And then embed the larger nums1 Behind
Time complexity :O(m+n)
Spatial complexity :O(l)
Code
Solution 1 : Double finger needling ( Write point redundancy )
public static void merge(int[] nums1, int m, int[] nums2, int n) {
int[] arr=new int[m+n];
int i=0;
int j=0;
int r=0;
if(m!=0 && n!=0){
while(i<m && j<n){
if((nums1[i]<=nums2[j])){
arr[r]=nums1[i];
r++;
i++;
}else{
arr[r]=nums2[j];
r++;
j++;
}
}
if(i<m){
for(;i<m;i++)
arr[r++]=nums1[i];
}
if(j<n){
for (;j<n;j++)
arr[r++]=nums2[j];
}
for(int x=0;x<m+n;x++){
nums1[x]=arr[x];
}
}else if (m==0 && n!=0){
for (int c=0;c<n;c++)
nums1[c]=nums2[c];
}else{
}
}
Solution 2 : Direct merge sequence method
public static void merge2(int[] nums1, int m, int[] nums2, int n) {
for(int i=0;i<n;i++)
nums1[m+i]=nums2[i];
Arrays.sort(nums1);
}
Solution 3 : Reverse double pointer method
public static void merge3(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1, p2 = n - 1;
int tail = m + n - 1;
int cur;
while (p1 >= 0 || p2 >= 0) {
if (p1 == -1) {
cur = nums2[p2--];
} else if (p2 == -1) {
cur = nums1[p1--];
} else if (nums1[p1] > nums2[p2]) {
cur = nums1[p1--];
} else {
cur = nums2[p2--];
}
nums1[tail--] = cur;
}
}
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/merge-sorted-array
边栏推荐
- Cloud native essay using Hana expression database service on Google kubernetes cluster
- Deep learning (tentsorflow2. version) three good student performance problems (1)
- 字符串常量池、class常量池和运行时常量池
- Code statistics tools cloc and SCC
- 测试须知——常见接口协议解析
- The third-party extension package of thinkphp6.0 supports uploading to Alibaba cloud and qiniu cloud
- US President signs community safety act to deal with gun issue
- 国际化配置
- Dialog centered
- The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
猜你喜欢

國際化配置

druid数据源实现后台监控

thymeleaf中抽取公共片段

Configuration internationale

Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)

c语言语法基础之——指针(字符、一维数组) 学习

Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)

定制拦截器

SSM项目小例子,SSM整合图文详细教程

自动化测试——pytest本身及第三方模块介绍及使用
随机推荐
SQL function
Download MySQL database installation package website of each system and version
Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
2. 合并两个有序数组
install ompl. sh
存储过程测试入门案例
创建对象的时候堆内存的分配
Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)
Battery historian analyzes battery consumption
自动化测试——pytest框架介绍及示例
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
1. 两数之和(LeetCode题目)
WIN10系统实现Redis主从复制
Win10安装tensorflow-quantum过程详解
软件测试---如何选择合适的正交表
Deep learning (tentsorflow2. version) three good student performance problems (1)
Constraintlayout control uses full Raiders
LeetCode 958. Completeness checking of binary tree
Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
2021-11-29 quintic polynomial of trajectory planning