349. 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.
代码如下:
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的更多相关文章
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 ...
- [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 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 15. leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- 【leetcode】349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- 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] ...
- 【一天一道LeetCode】#349. Intersection of Two Arrays
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
随机推荐
- HDU 1754 单点更新,求区间最大值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 5382 莫比乌斯反演
题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那 ...
- MonoRail学习:可重复组件ViewComponents的使用
在MonoRail中我们可以定义一些可重用的组件,在其他需要使用的页面引入这个组件就可以了.有点相当于.NET中的自定义控件,可以节约代码,方便开发,提高重用性. 在MonoRail中把这一功能叫做V ...
- maven的简单安装与配置
什么是Maven? Maven可以被理解成"知识的积累",也可以被翻译为"专家".它是一个项目管理工具. 它的主要服务即源于java平台的项目构建.依赖管理和项 ...
- lib静态链接库,dll动态链接库,h文件
最近在弄摄像头,发现我在调用摄像头自带的函数的时候,库没连接上,于是经过高人指点,学习了一下lib静态链接库,dll动态链接库来补充一下自己的基础知识. 一.首先我们来介绍一下lib静态链接库. li ...
- php的查询数据
php中 连接数据库,通过表格形式输出,查询数据.全选时,下面的分选项都选中;子选项取消一个时,全选按钮也取消选中. <!DOCTYPE html PUBLIC "-//W3C//DT ...
- 顺序表(C++)
以下为数据结构中的顺序表实现代码,已测试能够运行.虽然说是C++版的,但是其实应该是C语言班的.C++应该是面向对象,用抽象方法实现,而以下代码是面向过程的,只是把C语言中的输入(scanf)和输出( ...
- 文件操作I
<html> <head> <meta charset="utf-8"> </head> <body> <?php ...
- PHP中的正则表达式的使用
PHP中的正则表达式基础知识1.正则表达式就是描述字符串排列模式的一种自定义语法规则2.如果可以使用字符串处理函数完成的任务,就不使用正则表达式3.有一些复杂的操作,只能使用正则表达式完成4.正则表达 ...
- VIM_插件
VIM进阶:插件 通过一段时间的练习,你就可以非常熟练的使用VIM.即使是"裸奔",VIM已经足够强大,能够完成日常的绝大部分工作. 但VIM更加强大的是它的扩展机 ...