当前位置:网站首页>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];
}
}
}
边栏推荐
- PR notes:
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- Chapter 1: find the factorial n of n!
- PR 2021 quick start tutorial, how to create a new sequence and set parameters?
- Part 27 supplement (27) buttons of QML basic elements
- 2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
- CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
- Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
- Micro service knowledge sorting - cache technology
- Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
猜你喜欢
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
Derivation of decision tree theory
Virtual machine installation deepin system
PR FAQ: how to set PR vertical screen sequence?
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
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)
Use of aggregate functions
Machine learning support vector machine SVM
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Chapter 1: extend the same code decimal sum s (D, n)
随机推荐
Print linked list from end to end
6. Data agent object Defineproperty method
Acquisition and transmission of parameters in automatic testing of JMeter interface
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
44. Concurrent programming theory
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
PR notes:
FAQs for datawhale learning!
2. Template syntax
How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
Pat grade B 1009 is ironic (20 points)
Promethus
How can the outside world get values when using nodejs to link MySQL
Implementation of stack
Teach you how to quickly recover data by deleting recycle bin files by mistake
HCIA-USG Security Policy
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
7. Data broker presentation
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration