Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (smart0eddie)時間1年前 (2024/07/02 10:32), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串435/1554 (看更多)
20240702 350. Intersection of Two Arrays II Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order. 要找兩個array共通的element 所以其中一個先計數 然後另一個逐一比對是否有在第一個出現 vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { int maxV1 = *max_element(nums1.begin(), nums1.end()); cout << maxV1 << endl; vector<int> count(maxV1 + 1); for (int num : nums1) { count[num]++; } // for (int i = 0; i < maxV1 + 1; ++i) { // cout << i << ", " << count[i] << "," << endl; // } vector<int> inter; for (int num : nums2) { if (num <= maxV1 && count[num]) { inter.push_back(num); count[num]--; } } return inter; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719887535.A.527.html

07/02 10:32, 1年前 , 1F
大師
07/02 10:32, 1F

07/02 10:36, 1年前 , 2F
大師
07/02 10:36, 2F
文章代碼(AID): #1cWsQlKd (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cWsQlKd (Marginalman)