当前位置:网站首页>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];
}
}
}
边栏推荐
- Nerfplusplus parameter format sorting
- Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
- Initialization and instantiation
- FPGA learning notes: vivado 2019.1 project creation
- Rd file name conflict when extending a S4 method of some other package
- Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
- 2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- Acquisition and transmission of parameters in automatic testing of JMeter interface
- 7. Data broker presentation
- 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)
猜你喜欢

2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】

2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)

2.5 conversion of different data types (2)

Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation

Promethus

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

Nerfplusplus parameter format sorting
![Oak-d raspberry pie cloud project [with detailed code]](/img/34/76b461bf03fba373da5b5898c5204c.jpg)
Oak-d raspberry pie cloud project [with detailed code]

Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system

JMeter connection database
随机推荐
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
4. Data splitting of Flink real-time project
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
FPGA 学习笔记:Vivado 2019.1 工程创建
原生表格-滚动-合并功能
CMD implements the language conversion of locale non Unicode programs
Win10 share you don't have permission
Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
How can the outside world get values when using nodejs to link MySQL
Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a
IP address is such an important knowledge that it's useless to listen to a younger student?
Leetcode daily question solution: 540 A single element in an ordered array
Use of aggregate functions
An old programmer gave it to college students
The simplicity of laravel
1.4 learn more about functions
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
[raid] [simple DP] mine excavation
Rad+xray vulnerability scanning tool