当前位置:网站首页>Day6 merge two ordered arrays
Day6 merge two ordered arrays
2022-07-03 20:05:00 【Spare time life of pigs and shrimps】
// 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 .
// You can design and implement a time complexity of O(m + n)
#include<stdio.h>
#include <time.h>
#include <cstdlib>
#include <string.h>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
// The most direct conclusion is : Directly store the two arrays together , Sorting can be
int main()
{
int nums1[6] = {
4, 0, 0, 0, 0, 0 }, m =1, nums2[5] = {
1, 2, 3, 5, 6 }, n = 5;
int nums_index = 0;
for (int i = m; i < m+n; i++)
{
nums1[i] = nums2[nums_index];
nums_index++;
}
// array ( Insertion method , Sort from small to large )
for (int i = 1; i < m+n; i++)
{
int t = nums1[i];
int j = i - 1;
while (j >= 0 && nums1[j] > t)
{
nums1[j + 1] = nums1[j];
nums1[j] = t;
j--;
}
}
for (int i = 0; i < m + n; i++)
{
printf("%d ", nums1[i]);
}
return 0;
}
for leecode
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int nums_index = 0;
if(m != 0 & n != 0)
{
for (int i = m; i < m+n; i++)
{
nums1[i] = nums2[nums_index];
nums_index++;
}
// array ( Insertion method , Sort from small to large )
for (int i = 1; i < m+n; i++)
{
int t = nums1[i];
int j = i - 1;
while (j >= 0 && nums1[j] > t)
{
nums1[j + 1] = nums1[j];
nums1[j] = t;
j--;
}
}
}
else if(m == 0 & n != 0)
{
for (int i = 0; i < m+n; i++)
{
nums1[i] = nums2[i];
}
}
}
边栏推荐
- Microservice framework - frequently asked questions
- PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
- Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- Parental delegation mechanism
- Point cloud data denoising
- IP address is such an important knowledge that it's useless to listen to a younger student?
- About callback function and hook function
- 2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- kubernetes集群搭建efk日志收集平台
猜你喜欢

Acquisition and transmission of parameters in automatic testing of JMeter interface

Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que

IPv6 experiment
![[effective Objective-C] - block and grand central distribution](/img/09/22b979b97ea13d649b4b904637b79f.jpg)
[effective Objective-C] - block and grand central distribution
![[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University](/img/95/5baf5c8bedb00e67394a6c0a8234ff.png)
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University

2.4 conversion of different data types

Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval

Nerfplusplus parameter format sorting

Xctf attack and defense world crypto master advanced area olddriver

Chapter 1: find the factorial n of n!
随机推荐
Wechat applet quick start (including NPM package use and mobx status management)
Use of CMD command
Microservice knowledge sorting - search technology and automatic deployment technology
Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
Typora charges, WTF? Still need support
2.5 conversion of different data types (2)
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
Camera calibration (I): robot hand eye calibration
How to read the source code [debug and observe the source code]
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
NFT without IPFs and completely on the chain?
Print linked list from end to end
About unregistered transfer login page
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
CMD implements the language conversion of locale non Unicode programs
Win10 share you don't have permission
Realize user registration and login
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
Microservice framework - frequently asked questions
PR 2021 quick start tutorial, material import and management