当前位置:网站首页>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];
}
}
}
边栏推荐
- Micro service knowledge sorting - three pieces of micro Service Technology
- Virtual machine installation deepin system
- Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
- PR 2021 quick start tutorial, material import and management
- Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
- BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
- Rd file name conflict when extending a S4 method of some other package
- Kubernetes cluster builds efk log collection platform
- Cross compile opencv with contrib
- unittest框架基本使用
猜你喜欢

Commands related to files and directories

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

Acquisition and transmission of parameters in automatic testing of JMeter interface

IPv6 experiment
![Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively](/img/c5/0081689817700770f6210d50ec4e1f.png)
Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively

2.5 conversion of different data types (2)

HCIA-USG Security Policy

FPGA 学习笔记:Vivado 2019.1 工程创建

BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
![How to read the source code [debug and observe the source code]](/img/0d/6495c5da40ed1282803b25746a3f29.jpg)
How to read the source code [debug and observe the source code]
随机推荐
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
How to read the source code [debug and observe the source code]
Basic command of IP address configuration ---ip V4
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
Rad+xray vulnerability scanning tool
Typora charges, WTF? Still need support
44. Concurrent programming theory
unittest框架基本使用
10 smart contract developer tools that miss and lose
Chapter 1: seek common? Decimal and S (D, n)
An old programmer gave it to college students
6. Data agent object Defineproperty method
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
原生表格-滚动-合并功能
P5.js development - setting
Teach you how to quickly recover data by deleting recycle bin files by mistake
Microservice knowledge sorting - search technology and automatic deployment technology
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
Upgrade PIP and install Libraries