In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to solve the problem of merging two ordered arrays in leetcode, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
Topic link
Https://leetcode-cn.com/problems/merge-sorted-array/
Topic description
Given two ordered integer arrays nums1 and nums2, merge nums2 into nums1 so that num1 becomes an ordered array.
Description:
The number of elements initializing nums1 and nums2 is m and n, respectively.
You can assume that nums1 has enough space (the space size is greater than or equal to m + n) to hold the elements in nums2.
Example:
Enter:
Nums1 = [1, 2, 3, 0, 0, 0], m = 3
Nums2 = [2, 5, 5, 6], n = 3
Output: [1, 2, 2, 2, 3, 5, 6]
The idea of solving the problem
Tags: traversing the array from back to front
Because the space of the nums1 is concentrated at the back, it is better to process the sorted data from back to front, saving space and filling in values while traversing
Set the pointers len1 and len2 to point to the numeric tail of nums1 and nums2 respectively, compare the traversal starting from the tail value, and set the pointer len to the end of nums1. After each traversal compares the value, fill it.
When len1= 0 & & len2 > = 0) {
/ / Note-- the symbol comes after, indicating that the calculation is performed first and then minus 1, which shortens the code.
Nums1 [len--] = nums1 [len1] > nums2 [len2]? Nums1 [len1--]: nums2 [len2--]
}
/ / means to copy the nums2 array to the nums1 array starting from the subscript 0 position, starting from the subscript 0 position with a length of len2+1
System.arraycopy (nums2, 0, nums1, 0, len2 + 1)
}
}
JavaScript version
/ * *
* @ param {number []} nums1
* @ param {number} m
* @ param {number []} nums2
* @ param {number} n
* @ return {void} Do not return anything, modify nums1 in-place instead.
, /
Var merge = function (nums1, m, nums2, n) {
Let len1 = m-1
Let len2 = n-1
Let len = m + n-1
While (len1 > = 0 & & len2 > = 0) {
/ / Note-- the symbol comes after, indicating that the calculation is performed first and then minus 1, which shortens the code.
Nums1 [len--] = nums1 [len1] > nums2 [len2]? Nums1 [len1--]: nums2 [len2--]
}
Function arrayCopy (src, srcIndex, dest, destIndex, length) {
Dest.splice (destIndex, length,... src.slice (srcIndex, srcIndex + length))
}
/ / means to copy the nums2 array to the nums1 array starting from the subscript 0 position, starting from the subscript 0 position with a length of len2+1
ArrayCopy (nums2, 0, nums1, 0, len2 + 1)
}
Drawing and interpretation
These are all the contents of the article "how to solve the problem of merging two ordered arrays in leetcode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.