In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the C++ how to achieve mixed insertion of ordered array related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this C++ article on how to achieve mixed insertion of ordered array will have something to gain, let's take a look.
Merge Sorted Array mixed insert ordered array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
The number of elements initialized in nums1and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
Nums1 = [1, 2, 3, 0, 0, 0], m = 3
Nums2 = [2, 5, 5, 6], n = 3
Output: [1,2,2,3,5,6]
Mixed insert ordered arrays, since both arrays are ordered, all you have to do is to compare the sizes in order. The title said that the nums1 array has enough space, indicating that the resize array is not used, and m and n are given, then you know the size of the mixed array, so you start a comparison from the end of the nums1 and nums2 array, adding the larger numbers to the end of the mixed array from back to front in order. You need three variables iQuery jjjjr k, pointing to nums1,nums2, and the end of the mixed array. While loop, if I and j are greater than 0, and then if nums1 [I] > nums2 [j], it means that nums1 [I] should be added to the end of the mixed array first, and k and I will be subtracted by 1; otherwise, nums2 [j] will be added to the end of the mixed array, and k and j will be subtracted by 1. At the end of the loop, it is possible that I or j is greater than or equal to 0, if j is greater than 0, then you need to continue the loop to copy the numbers in nums2 into nums1. If I is greater than or equal to 0, then it doesn't matter, because the mixed array itself is placed in nums1, as shown in the following code:
Solution 1:
Class Solution {public: void merge (vector& nums1, int m, vector& nums2, int n) {int I = m-1, j = n-1, k = m + n-1; while (I > = 0 & & j > = 0) {if (nums1 [I] > nums2 [j]) nums1 [nums1 -] = nums1 [I -]; else nums1 [nums1 -] = nums2 [nums1 -] } while (j > = 0) nums1 [Kmuri -] = nums2 [jmurf -];}}
We can also write more succinctly and merge the two while loops together. As long as we add the judgment condition of I > = 0 and nums1 [I] > nums2 [j], we can take the number from the nums1, otherwise we will always take the number from the nums2, as shown in the code below:
Solution 2:
Class Solution {public: void merge (vector& nums1, int m, vector& nums2, int n) {int I = m-1, j = n-1, k = m + n-1; while (j > = 0) {nums1 [nums1 -] = (I > = 0 & & nums1 [I] > nums2 [j])? Nums1 [iMurray -]: nums2 [jmurf -];}; this is the end of the article on "how C++ can mix and insert ordered arrays". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to mix and insert ordered arrays in C++". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.