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.

给出两个数组,求他们的交集。出题的意图何在?

    public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Set<Integer> inter = new HashSet<>();
for(int a:nums1)
set.add(a);
for(int a:nums2)
{
if(set.contains(a))
inter.add(a); }
int result[] = new int[inter.size()];
int i = 0;
for(int s:inter)
result[i++] = s;
return result;
}

思路简单,代码啰嗦,用python三行搞定

 def intersection(self, nums1, nums2):
a = set(nums1)
b = set(nums2)
return list(a & b);

Add to List 349. Intersection of Two Arrays的更多相关文章

  1. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  2. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  3. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. 349. Intersection of Two Arrays 是否重复

    不重复的: 方法一 [一句话思路]:排序之后用归并. [一刷]: 根据返回方法的类型来判断corner case.判断空.0数组的corner case还不一样,得分开写 由于先排序了,nums1[i ...

  6. [leetcode]349. Intersection of Two Arrays数组交集

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  7. LeetCode 349. Intersection of Two Arrays (两个数组的相交)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  8. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  9. LeetCode 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

随机推荐

  1. laravel MethodNotAllowedHttpException错误一个原因

    前两天在写api的时候,出现一个之前都没有碰到过的问题,如图 可以说提示信息是很不友好了,然后打开错误日志,发现报了一个MethodNotAllowedHttpException这样的错误,这样错误我 ...

  2. SVN服务器

    什么是SVN服务器? 1.协助多人开发的代码管理器: 2.会记录代码是谁写的,并且可以回退到任意版本: 3.冲突解决: 4.升级(项目分支). SVN的搭建: 1.在服务器端下载SVN服务器,一个项目 ...

  3. springboot-helloworld

    1使用idea创建springboot项目如下图所示 并选择web模块 2,登录springboot官网 http://projects.spring.io/spring-boot/ 引入相关依赖包如 ...

  4. 简单使用Unity导航系统(小白之路)

    1.介绍 NavMesh:是一种根据场景中几何图像创建出来的3D网格.它会使导航和寻路变得很容易. 简单来说,NavMesh是一种我们在游戏世界中,可以让游戏角色在其表面行走并且导航的平面. 2.注意 ...

  5. 51Nod--1006 lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  6. 【JDK1.8】JDK1.8集合源码阅读——IdentityHashMap

    一.前言 今天我们来看一下本次集合源码阅读里的最后一个Map--IdentityHashMap.这个Map之所以放在最后是因为它用到的情况最少,也相较于其他的map来说比较特殊.就笔者来说,到目前为止 ...

  7. 转自知乎-wifi破解

    上点python有意思的代码. 环境准备 python2.7 pywifi模块 字典 清除系统中的任何wifi连接记录 导入模块 这里用的模块就这三个 pywifi的_wifiutil_linux.p ...

  8. Uva 12436 Rip Van Winkle&#39;s Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  9. LintCode-落单的数 III

    给出2*n + 2个的数字.除当中两个数字之外其它每一个数字均出现两次,找到这两个数字. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 [1,2,2,3,4,4,5,3].返回 1和5 挑战 ...

  10. Golang开发环境搭建(Notepad++、LiteIDE两种方式以及martini框架使用)

    本文介绍两种Golang的开发环境一种基于notepad++.还有一种基于liteide. 1.下载Golang语言的pkg:http://golangtc.com/download 直接点击安装,一 ...