Re: [闲聊] 每日leetcode

楼主: smart0eddie (smart0eddie)   2024-07-02 10:32:13
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]
作者: sustainer123 (caster)   2023-07-02 10:32:00
大师
作者: Furina (芙宁娜)   2024-07-02 10:36:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com