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 class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<nums1.length;i++)
{
for(int j=0;j<nums2.length;j++)
{
if(nums1[i]==nums2[j])
{
if(!list.contains(nums1[i]))
list.add(nums1[i]);
}
}
}
int[] result=new int[list.size()];
for(int i=0;i<list.size();i++)
result[i]=list.get(i);
return result;
}
}

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. LeetCode 349. Intersection of Two Arrays

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

  5. 15. leetcode 349. Intersection of Two Arrays

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

  6. 【leetcode】349. Intersection of Two Arrays

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

  7. Add to List 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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

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

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

随机推荐

  1. iOS8 针对开发者所拥有的新特性汇总如下

    iOS8 针对开发者所拥有的新特性汇总如下 1.支持第三方键盘 2.自带网页翻译功能(即在线翻译) 3.指纹识别功能开放:第三方软件可以调用 4.Safari浏览器可直接添加新的插件. 5.可以把一个 ...

  2. hdu 4619 Warm up 2

    http://acm.hdu.edu.cn/showproblem.php?pid=4619 根据题意可知,每一个方格可能只被一个骨牌覆盖 可能被两个骨牌覆盖 也可能不被覆盖 有一个骨牌覆盖的方格(单 ...

  3. 一模 (2) day2

    第一题: 题目大意:给出n种物品和每种物品的件数,求拿k件的方案数.N<=30 解题过程: 1.一开始总想着是组合数学的模型,结果怎么都想不出来..然后写了个爆搜,数据很弱,只有1个点超时. 2 ...

  4. T-sql语句

    在用代码编辑数据库时,首先要启动WAMPSERVER 1.创建表 create table Car ( Code varchar(50) primary key , Name varchar(50) ...

  5. acm 20140825

    为了自己的梦想,一次次的选择坚强.走上acm这条路,怎么也找不到让自己放弃的理由.我喜欢这种竞赛的氛围,我渴望在赛场上飞扬!想想过去的一个学习,自己并没有干点什么有意义的事.acm也没有好好的做!新的 ...

  6. D.T SOFTWARE (1) 软件架构直播答疑课程

    今晚的d.t课程 1项目需求 PPTP服务搭建完成PPTP服务器的搭建,用户重新拨号获得新IP后,要求拔PPTP VPN成功时,也获取到新的公网IP,而且能通过代理上网.VNC服务安装用户可以通过VN ...

  7. static声明初始化块的一下注意事项

    通过输出结果,我们可以看到,程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法.由于静态初始化块只在类加载时执行一次,所以当再次创建对象 hello2 时并未执行静态初始化块.

  8. Ubuntu 14.10 下Server版本中文乱码问题

    在安装Ubuntu server 14.10 时候选择了中文版,但是装好系统发现,里面的中文有乱码,解决办法 打开 /etc/default/locale sudo nano /etc/default ...

  9. jsp弹出Please check the location and try again!对话框

    关闭它的jsp图形模式.myeclipse10中打开jsp文件时,右键open with 选MyEclipse JSP Editor,不选MyEclipse Visual JSP Editor模式.

  10. CRM客户关系管理系统(十三)

    ---客户资料添加 1.事件流程: