标签:
Given two arrays, write a function to compute their intersection.
Example: Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
题目大意:找出两个数组交叉的数
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 };
Leetcode 349. Intersection of Two Arrays
标签:
原文地址:http://www.cnblogs.com/qinduanyinghua/p/5509983.html