当前位置:网站首页>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];
}
}
}
边栏推荐
- 2.3 other data types
- Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- Xctf attack and defense world crypto master advanced area olddriver
- 6006. Take out the minimum number of magic beans
- Use of CMD command
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- Today's work summary and plan: February 14, 2022
- Point cloud data denoising
猜你喜欢

BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply

Network security Kali penetration learning how to get started with web penetration how to scan based on nmap

Native table - scroll - merge function

Promethus

Use of aggregate functions

Test changes in Devops mode -- learning and thinking

PR 2021 quick start tutorial, material import and management

Change deepin to Alibaba image source

Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520

2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
随机推荐
Cross compile opencv with contrib
1.5 learn to find mistakes first
[effective Objective-C] - block and grand central distribution
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
Basic command of IP address configuration ---ip V4
Microservice knowledge sorting - search technology and automatic deployment technology
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
Print linked list from end to end
Chapter 1: sum of three factorials, graph point scanning
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Nacos usage of micro services
Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
2.3 other data types
Difference between surface go1 and surface GO2 (non professional comparison)
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
6006. Take out the minimum number of magic beans
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
Oak-d raspberry pie cloud project [with detailed code]
The 29th day of force deduction (DP topic)
Point cloud data denoising