Skip to content

Commit f5cbb54

Browse files
authored
Create 350. Intersection of Two Arrays II
1 parent c490ded commit f5cbb54

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

350. Intersection of Two Arrays II

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
4+
sort(nums1.begin(),nums1.end());
5+
sort(nums2.begin(),nums2.end());
6+
int i=0,j=0;
7+
vector<int>ans;
8+
while(i<nums1.size() && j<nums2.size()){
9+
if(nums1[i]==nums2[j]){
10+
ans.push_back(nums1[i]);
11+
i++;
12+
j++;
13+
}else if(nums1[i]>nums2[j]){
14+
j++;
15+
}else{
16+
i++;
17+
}
18+
}
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)