-
Notifications
You must be signed in to change notification settings - Fork 9
/
350IntersectionOfTwoArraysII.cpp
44 lines (38 loc) · 1.08 KB
/
350IntersectionOfTwoArraysII.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> intersect(vector<int> &nums1, vector<int> &nums2)
{
vector<int> result;
vector<int> *num_short_p = &nums1;
vector<int> *num_long_p = &nums2;
if (nums1.size() > nums2.size())
{
num_short_p = &nums2;
num_long_p = &nums1;
}
vector<int> &num_short = *num_short_p;
vector<int> &num_long = *num_long_p;
int n = num_long.size();
for (int i = 0; i < num_short.size(); ++i)
{
for (int j = 0; j < n; ++j)
{
if (num_short[i] == num_long[j])
{
result.push_back(num_short[i]);
// swap with the last one
int temp = num_long[j];
num_long[j] = num_long[n - 1];
num_long[n - 1] = temp;
// make sure to search from [0, n]
--n;
break;
}
}
}
return result;
}
};