当前位置:网站首页>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());
}
};
边栏推荐
- Sorry, this user does not exist!
- NG Textarea-auto-resize
- 期末复习(day3)
- Configure DTD of XML file
- NG Textarea-auto-resize
- Ansible firewall firewalld setting
- pytorch DataLoader实现miniBatch(未完成)
- Qt读写Excel--QXlsx插入图表5
- Altaro requirements for starting from backup on Hyper-V
- Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
猜你喜欢
![[together Shangshui Shuo series] day 7 content +day8](/img/fc/74b12addde3a4d3480e98f8578a969.png)
[together Shangshui Shuo series] day 7 content +day8

伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)

今天很多 CTO 都是被幹掉的,因為他沒有成就業務
![[teacher Zhao Yuqiang] use the catalog database of Oracle](/img/0b/73a7d12caf955dff17480a907234ad.jpg)
[teacher Zhao Yuqiang] use the catalog database of Oracle

Why is the website slow to open?

Latest version of source insight

Map的扩容机制

Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning

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

一起上水硕系列】Day 9
随机推荐
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
Using the ethtool command by example
The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
1. 两数之和
How to create and configure ZABBIX
求质数的方法
2022.7.2 模拟赛
Mapbox tasting value cloud animation
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
期末复习(Day5)
70 shell script interview questions and answers
【无标题】
牛客网 JS 分隔符
一起上水硕系列】Day 9
Life is a process of continuous learning
Source insight automatic installation and licensing
Together, Shangshui Shuo series] day 9