当前位置:网站首页>88. Merge two ordered arrays
88. Merge two ordered arrays
2022-07-03 05:48:00 【yitahutu79】
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 .
Example 1:
Input :nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output :[1,2,2,3,5,6]
explain : Need merger [1,2,3] and [2,5,6] .
The combined result is [1,2,2,3,5,6] , In which, bold italics indicates nums1 The elements in .
Example 2:
Input :nums1 = [1], m = 1, nums2 = [], n = 0
Output :[1]
explain : Need merger [1] and [] .
The combined result is [1] .
Example 3:
Input :nums1 = [0], m = 0, nums2 = [1], n = 1
Output :[1]
explain : The array to be merged is [] and [1] .
The combined result is [1] .
Be careful , because m = 0 , therefore nums1 No elements in .nums1 The only remaining 0 Just to ensure that the merged results can be successfully stored in nums1 in .
Tips :
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[j] <= 109
Method 1 : Double finger needling
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int arr[205] = {
0};
for (int i = 0, n1 = 0, n2 = 0; i < m + n; i++){
if (n1 == m) arr[i] = nums2[n2++];
else if(n2 == n) arr[i] = nums1[n1++];
else if (nums1[n1] < nums2[n2]) arr[i] = nums1[n1++];
else arr[i] = nums2[n2++];
}
for (int i = 0, j = 0; i < m + n; i++) {
nums1[i] = arr[j++];
}
}
};
Method 2 :sort Sort
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int i = 0; i < n; i++) {
nums1[m + i] = nums2[i];
}
sort(nums1.begin(), nums1.end());
}
};
边栏推荐
- 伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)
- 理解 YOLOV1 第一篇 预测阶段
- [together Shangshui Shuo series] day 7 content +day8
- Ensemble, série shuishu] jour 9
- [advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
- ansible防火墙firewalld设置
- Sophomore dilemma (resumption)
- "C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
- [minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
- Apache+PHP+MySQL环境搭建超详细!!!
猜你喜欢

"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
![[teacher Zhao Yuqiang] MySQL high availability architecture: MHA](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
![[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence

【一起上水硕系列】Day 10
![[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation](/img/c2/991b8febd262cf9237017adc9d1221.jpg)
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
![[teacher Zhao Yuqiang] use the catalog database of Oracle](/img/0b/73a7d12caf955dff17480a907234ad.jpg)
[teacher Zhao Yuqiang] use the catalog database of Oracle

深度学习,从一维特性输入到多维特征输入引发的思考

Apache+php+mysql environment construction is super detailed!!!

Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

Today, many CTOs were killed because they didn't achieve business
随机推荐
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
Crontab command usage
Installation du plug - in CAD et chargement automatique DLL, Arx
Redis encountered noauth authentication required
Can altaro back up Microsoft teams?
Mapbox tasting value cloud animation
獲取並監控遠程服務器日志
70 shell script interview questions and answers
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Kubernetes resource object introduction and common commands (V) - (configmap)
Source insight License Activation
C 语言文件操作函数大全 (超详细)
大二困局(复盘)
Redhat7 system root user password cracking
[teacher Zhao Yuqiang] use Oracle's tracking file
ansible防火墙firewalld设置
Get and monitor remote server logs
[untitled]
[set theory] relational closure (reflexive closure | symmetric closure | transitive closure)
Final review (Day2)