LeetCode: Intersection of Two Arrays
来源:互联网 发布:java 启动main 编辑:程序博客网 时间:2024/05/18 03:05
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 orde
使用set代码
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> s1; set<int> s2; int n1 = nums1.size(); int n2 = nums2.size(); for (int i = 0; i < n1; ++i) s1.insert(nums1[i]); for (int i = 0; i < n2; ++i) s2.insert(nums2[i]); vector<int> res; set<int>::iterator it1 = s1.begin(); set<int>::iterator it2 = s2.begin(); while(it1 != s1.end() && it2 != s2.end()) { if (*it1 == *it2) { res.push_back(*it1); it1++; it2++; } else if (*it1 < *it2) it1++; else it2++; } return res; } };
1 0
- [LeetCode] Intersection of Two Arrays
- LeetCode Intersection of Two Arrays
- LeetCode:Intersection of Two Arrays
- 【leetcode】Intersection of Two Arrays
- leetcode Intersection of Two Arrays
- 【leetcode】Intersection of Two Arrays
- leetcode:Intersection of Two Arrays
- [Leetcode]Intersection of Two Arrays
- LeetCode:Intersection of Two Arrays
- LeetCode: Intersection of Two Arrays
- leetcode ----Intersection of Two Arrays
- LeetCode Intersection of Two Arrays
- [Leetcode] Intersection of Two Arrays
- LEETCODE--Intersection of Two Arrays
- LeetCode : Intersection of Two Arrays
- leetcode---Intersection of Two Arrays
- leetCode---Intersection of Two Arrays
- leetcode[Intersection of Two Arrays]
- HDU 5723-A - Abandoned country-最小生成树+树dp
- chapter 1 let 和 const
- HDU 1285 确定比赛名次【拓扑排序】
- 关于JavaScript设计模式1
- UDP协议点对点(P2P)通讯(或者说NAT穿越)实例
- LeetCode: Intersection of Two Arrays
- Spring系列-Spring介绍
- POJ1458 Common Subsequence
- 如何实现生产者消费者模式
- GNU make - functions
- javaweb—HttpServletRequest和HttpServletResponse对象
- hdu 5748 Bellovin【nlogn最长递增子序列】
- charles使用
- HDU 1811 Rank of Tetris 【并查集+拓扑排序】