当前位置:网站首页>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());
}
};
边栏推荐
- 配置xml文件的dtd
- How to create and configure ZABBIX
- [teacher Zhao Yuqiang] kubernetes' probe
- Source insight automatic installation and licensing
- Life is a process of continuous learning
- Redhat7系统root用户密码破解
- MySQL startup error: several solutions to the server quit without updating PID file
- Brief introduction of realsense d435i imaging principle
- [teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
- Notepad++ wrap by specified character
猜你喜欢
![[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system

Redhat7 system root user password cracking

Capacity expansion mechanism of map
![[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation](/img/c2/991b8febd262cf9237017adc9d1221.jpg)
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
![[trivia of two-dimensional array application] | [simple version] [detailed steps + code]](/img/84/98c1220d0f7bc3a948125ead6ff3d9.jpg)
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
![[branch and cycle] | | super long detailed explanation + code analysis + a trick game](/img/aa/543d4f0dcbcd664be963579af77ec9.jpg)
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
![[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis](/img/df/884313a69fb1e613aec3497800f7ba.jpg)
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis

Today, many CTOs were killed because they didn't achieve business

Map的扩容机制
![[teacher Zhao Yuqiang] index in mongodb (Part 2)](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
随机推荐
【无标题】
一起上水碩系列】Day 9
[teacher Zhao Yuqiang] Flink's dataset operator
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Troubleshooting of 32GB Jetson Orin SOM failure to brush
Solve the problem of automatic disconnection of SecureCRT timeout connection
Crontab command usage
Installation of CAD plug-ins and automatic loading of DLL and ARX
Final review (Day7)
期末复习(day3)
How does win7 solve the problem that telnet is not an internal or external command
Detailed explanation of iptables (1): iptables concept
Ansible firewall firewalld setting
If function of MySQL
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
配置xml文件的dtd
理解 YOLOV1 第一篇 预测阶段
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)