350. Intersection of Two Arrays II
来源:互联网 发布:js九九乘法表代码 编辑:程序博客网 时间:2024/06/13 05:40
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
先排序,然后双指针同时遍历两个数组。
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { if (nums1.size()>nums2.size()) { return intersect(nums2, nums1); } vector<int> result; sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); auto it1 = nums1.begin(), it2 = nums2.begin(); while (it1 != nums1.end() && it2 != nums2.end()) { if (*it1 < *it2) { ++it1; } else if (*it1 > *it2) { ++it2; } else { result.push_back(*it1); ++it1, ++it2; } } return result; }};
0 0
- 350. Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- leetcode.350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II【E】
- 350. Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 【leetcode】350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- jquery在选择元素的时候,可以写成var div=$("div")
- 关于AndroidStudio的Oracle VM VirtualBox和Genymotion配置问题(Win10系统)
- Flex 布局教程:语法篇
- 单例传值
- ubuntu server 无法使用无线网络
- 350. Intersection of Two Arrays II
- android开发之-相对布局的属性
- 谈谈CAN-BUS和LIN-BUS
- m2e安装
- Git HEAD 意思详解 和版本回退
- Hibernate distinct 查询的多种实现方式
- hdu 5631 Rikka with Graph(并查集)
- java
- 二叉树的重建