In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you "how C++ solves the intersection of two arrays". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "how C++ solves the intersection of two arrays".
First, the topic description given two arrays, write a function to calculate their intersection. Example 1: input: nums1 = [1Peragne 2magor2], nums2 = [2Pere2] output: [2Pere2] example 2: input: nums1 = [4pje 9je 5], nums2 = [9pje 4pl 8je 4] output: [4pc9] Note: the number of occurrences of each element in the output result should be consistent with the minimum number of occurrences of the element in the two arrays. We can ignore the order of the output results. Second, the idea of solving the problem (1) Hash table search
Ideas for solving the problem:
Use a hash table to record the number of occurrences of each element in an array, myMap {element: number of element occurrences}
Iterate through another array, and when the current element exists in the hash table, the count of the corresponding element is subtracted by 1, and the element is stored in res.
C++ is implemented as follows:
Class Solution {public:vector intersect (vector& nums1, vector& nums2) {if (nums1.empty () | | nums2.empty ()) return {}; / / Hash table records the number of occurrences of each element in the hash table unordered_map myMap;for (auto & num: nums1) myMap [num] + +; vector res;// traversal array 2for (int I = 0; I
< nums2.size(); ++i ){ // 判断数组2和集合1是否有公共元素if( myMap.count(nums2[i]) ){ // 找到后,则对应的元素次数减1if( myMap[nums2[i]] >0) res.emplace_back (nums2 [I]); / / reduce the number of times in set 1 myMap [nums2 [I]] -;}} return res;}}
Running result:
Complexity analysis:
Time complexity: two for () loops are used and are related to each other, so the overall time complexity is O (n), where m and n are the lengths of two arrays respectively.
Space complexity: a new set myMap is defined to store elements in the array nums1, so the space complexity is also O (n) or O (m).
(2) sort + double pointer
Ideas for solving the problem:
First sort the two arrays, and then use two pointers to traverse the two arrays
Initially, the two pointers point to the heads of the two arrays, and the numbers in the two arrays pointed to by the two pointers are compared each time
If the two numbers are not equal, the pointer to the smaller number is moved one bit to the right, if the two numbers are equal, the number is added to the answer, and both pointers are moved one bit to the right.
Traversal ends when at least one pointer is out of range of the array.
C++ is implemented as follows:
Class Solution {public:vector intersect (vector& nums1, vector& nums2) {/ / sort sort (nums1.begin (), nums1.end ()); sort (nums2.begin (), nums2.end ()); / / get array length int len1 = nums1.size (); int len2 = nums2.size (); / / initial pointer int index1=0, index2=0;vector res;while (index1
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.