350. Intersection of Two Arrays II
来源:互联网 发布:视频机器码破解软件 编辑:程序博客网 时间:2024/05/01 00:33
Problem:
Solution:
在“349. Intersection of Two Arrays”问题的基础上稍作变动,349中不会显示重复元素,此题所有重复的交集元素都必须显示。只需先对两数组进行排序,剩下的工作就简单很多了,可以在O(n)时间内完成。
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> res; int len1 = nums1.size(); int len2 = nums2.size(); sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); int i = 0; int j = 0; while (i < len1 && j < len2) { if (nums1[i] == nums2[j]) { res.push_back(nums1[i]); i++; j++; } else if (nums1[i] < nums2[j]) i++; else j++; } return res; }};
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
- Oracle04
- BIO,NIO和AIO的区别
- [java入门]--java第一个程序Hello World(超详细步骤)
- 292. Nim Game
- 复制带随机指针的链表
- 350. Intersection of Two Arrays II
- Struts2系列笔记(7)---Struts2类型转换
- C++ bitset类的使用与简介
- Pd10安装 Ubuntu14.04
- java.util.concurrent介绍
- WebLogic
- 基于S5PV210板驱动开发之按键驱动
- CPU、内存、硬盘、指令以及他们之间的关系
- windows redis设置日志路径,设置密码,Java使用密码连接redis