Leetcode 349. Intersection of Two Arrays
349. Intersection of Two Arrays
Total Accepted: 2944 Total Submissions: 6253 Difficulty: Easy
Given two arrays, write a function to compute their intersection.
Example: Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
题目大意:找出两个数组交叉的数
1 class Solution { 2 public: 3 vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { 4 set<int> s1, s2; 5 for(int i = 0; i < nums1.size(); i++){ 6 s1.insert(nums1[i]); 7 } 8 9 for(int i = 0; i < nums2.size(); i++){ 10 if(s1.find(nums2[i]) != s1.end()) 11 s2.insert(nums2[i]); 12 } 13 vector<int> v; 14 for(set<int>::iterator it = s2.begin(); it != s2.end(); it++){ 15 v.push_back(*it); 16 } 17 return v; 18 } 19 };
越努力,越幸运