作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/intersection-of-two-arrays/

  • Difficulty: Easy

题目描述

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:

  1. Each element in the result must be unique.
  2. The result can be in any order.

题目大意

找出两个数组中共同出现的数字,保证结果中返回的数字是唯一的,返回结果的顺序无所谓。

解题方法

方法一:Java解法,HashSet

每次都要看解析啊摔!用HashSet去除每个数组的重复元素,统计重复出现的元素即可。java只有元素的个数确定了才能新建数组,所以这里注意一下。

public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set1=new HashSet<Integer>();
for(int i : nums1){
set1.add(i);
}
HashSet<Integer> set2=new HashSet<Integer>();
for(int i: nums2){
if(set1.contains(i)){
set2.add(i);
}
}
int answer[]=new int[set2.size()];
int i=0;
for(int n:set2){
answer[i++]=n;
}
return answer;
}
}

AC:8 ms

方法二:Python解法,set

还是python对于这种多种数据结构的题处理来方便。打败96%的提交。

class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list(set(nums1) & set(nums2))

日期

2017 年 1 月 8 日
2018 年 11 月 11 日 —— 剁手节快乐

【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)的更多相关文章

  1. LeetCode 349 Intersection of Two Arrays 解题报告

    题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...

  2. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  3. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  4. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  6. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  7. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  9. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

随机推荐

  1. MEGAN4,MEGAN5和MEGAN6的Linux安装和使用

    目录 MEGAN 4 MEGAN 5 MEGAN 6 MEGAN(Metagenome Analyzer)是宏基因组学进行物种和功能研究的常用软件,实际上现在的Diamond+MEGAN6已经是一套比 ...

  2. Nginx编译安装相关参数

    Nginx编译安装相关参数 Nginx插件安装 ------------------pcre------------------ cd /usr/local/source wget http://ww ...

  3. 短序列组装Sequence Assembly(转载)

    转载:http://blog.sina.com.cn/s/blog_4af3f0d20100fq5i.html 短序列组装(Sequence assembly)几乎是近年来next-generatio ...

  4. Matlab | 绘制动态曲线(使用 animatedline 对象)

    效果如下: 示例代码: figure('Color','w'); h1 = animatedline; h1.Color = 'r'; h1.LineWidth = 1.0; h1.LineStyle ...

  5. List 去重的 6 种方法,这个方法最完美!

    在日常的业务开发中,偶尔会遇到需要将 List 集合中的重复数据去除掉的场景.这个时候可能有同学会问:为什么不直接使用 Set 或者 LinkedHashSet 呢?这样不就没有重复数据的问题了嘛? ...

  6. mybatis项目中,使用useSSL=true却报错

    今天在玩儿mybatis的时候遇到一个蛮有东西的事情:抛了一个让我折腾几个小时的错误,所以记录一下 这个错误有意思的地方就在于这里: 当使用useSSL安全连接时,抛出了上述的错误把useSSL改为f ...

  7. HTTP 之 options预请求

    一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...

  8. Android数据存取

    Android数据存取 一.SharedPreferencesc存取数据 SharedPreferences是使用键值对的方式来存储数据的,也就是在保存一条数据时,需要给这条数据提供一个对应的键,这样 ...

  9. 转Android service 启动篇之 startForegroundService

    本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...

  10. ebs 初始化登陆

    BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...