[LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起。
原题地址:
349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/
350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
题目&&解法:
1.Intersection of Two Arrays:
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.
这道题目要注意的就是不能重复。我采取的做法是遍历一遍nums1数组,然后和nums2数组比对,假如nums2里面存在并且要返回的数组中没有这个数值,就把他插入要返回的数组里面。很低端的一种做法,代码如下:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> temp;
for (int i = ; i < nums1.size(); i++) {
if (find(nums2.begin(), nums2.end(), nums1[i]) != nums2.end() && find(temp.begin(), temp.end(), nums1[i]) == temp.end()) {
temp.push_back(nums1[i]);
}
}
return temp;
}
};
2.Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
这道题目比上面的题目复杂了一点,它要求把重复的元素都放进返回的数组里面,我采取了一种非常非常垃圾的做法:先定义一个结构体,一个int类型和一个bool类型,int变量数值复制传入的数组,然后用bool变量标记当前元素的数值是否已经插入要返回的数组。然后采取双层循环逐个比对。代码如下:
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
struct v{
int data;
bool isChoosed;
};
vector<struct v> struct_num1;
vector<struct v> struct_num2;
for (int i = ; i < nums1.size(); i++) {
struct v t;
t.data = nums1[i];
t.isChoosed = false;
struct_num1.push_back(t);
}
for (int i = ; i < nums2.size(); i++) {
struct v t;
t.data = nums2[i];
t.isChoosed = false;
struct_num2.push_back(t);
}
vector<int> temp;
for (int i = ; i < nums1.size(); i++) {
for (int j = ; j < nums2.size(); j++) {
if (struct_num1[i].data == struct_num2[j].data && struct_num2[j].isChoosed == false && struct_num1[i].isChoosed == false) {
temp.push_back(struct_num2[j].data);
struct_num1[i].isChoosed = true;
struct_num2[j].isChoosed = true;
}
}
}
return temp;
}
};
这种做法让我鄙视我自己,时间复杂度为O(n^2),极高。而且写起来极其麻烦。肯定有简单的方法啊!
根据http://blog.csdn.net/yzhang6_10/article/details/51526070里面的一种比较快的思路:
(1)先对两个数组进行排序
(2)遍历两个数组,比较对应元素:若相等,两个数组的索引同时增加;若不等,较小元素的数组的索引增加。
这是一个极精妙的方法,个人感觉原理和归并数组有点相似。这个算法值得经常去回顾一下,特此记录。
代码如下:
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
vector<int> result;
for (int i = , j = ; i < nums1.size() && j < nums2.size(); )
{
if (nums1[i] == nums2[j])
{
result.push_back(nums1[i]);
i++;
j++;
}
else if (nums1[i] < nums2[j])
i++;
else if (nums1[i] > nums2[j])
j++;
}
return result;
}
};
[LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II的更多相关文章
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- LeetCode 349,350 数组的交集
LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
随机推荐
- yii2 Rbac使用yii命令一键建表
首先在web.php和console.php配置authmanager组件,web.php是应用配置文件,console.php是yii命令配置文件,都需要配置. 在components加入如下配置: ...
- Spring框架IOC,DI概念理解
1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...
- 【技术分享】手把手教你使用PowerShell内置的端口扫描器
[技术分享]手把手教你使用PowerShell内置的端口扫描器 引言 想做端口扫描,NMAP是理想的选择,但是有时候NMAP并不可用.有的时候仅仅是想看一下某个端口是否开放.在这些情况下,PowerS ...
- python+selenium自动化软件测试(第4章):场景判断与封装
4.1 显示等待WebDriverWait 前言:在脚本中加入太多的sleep后会影响脚本的执行速度,虽然implicitly_wait()这种隐式等待在一定程度上节省了很多时间.但是一旦页面上某些j ...
- NOIP[2015] Day2题解
问题 A: 跳石头 时间限制: 1 Sec 内存限制: 128 MB 题目描述 一年一度的"跳石头"比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石. ...
- macaca web(4)
米西米西滴,吃过中午饭来一篇,话说,上回书说道macaca 测试web(3),参数驱动来搞,那么有小伙本又来给雷子来需求, 登录模块能不能给我给重新封装一下吗, 我说干嘛封装,现在不挺好,于是乎,接着 ...
- 一步步带你做vue后台管理框架(二)——上手使用
系列教程<一步步带你做vue后台管理框架>第二课 github地址:vue-framework-wz 线上体验地址:立即体验 闲扯再多不会用也没白搭,这节课我来带大家直接上手框架,体验到简 ...
- windows.onload和 document.ready区别
在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...
- Redis在java中的使用
1.首先安装Redis环境.可以在Windows.linux.别的服务器上搭建Redis环境 2.在java工程中导入必须的jar包 如maven上导入两个jar] <!-- Redis必须包 ...
- Topshelf便捷创建Windows服务
结合Quartz.net学习,前提已经创建了一个定时任务,可见 <定时调度框架:Quartz.net> (基于配置文件形式) 首先引用Topshelf.dll 自定义服务TestServi ...