349. Intersection of Two Arrays

https://leetcode.com/problems/intersection-of-two-arrays/#/solutions

http://www.cnblogs.com/EdwardLiu/p/6096747.html

三种方法哦哦哦

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

时间: 2024-10-11 08:23:41

349. Intersection of Two Arrays的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 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/ 题目&解法

Leetcode刷题记录[python]——349 Intersection of Two Arrays

一.前言 做了两题才慢慢摸清了leetcode的操作. 二.题349 Intersection of Two Arrays Given two arrays, write a function to compute their intersection. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]

[leetcode] 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. 第一想法用HashMap<Integer, Boolean>,但错误,用两个

349. Intersection of Two Arrays【双指针|二分】

2017/3/23 15:41:47 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. 作弊版:Python classSolu

【LeetCode】 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]. 第一次做的时候先将nums1存储在一个unordered_map里,然后对nums2的元素判断是否在unordered_map中,如果在,放入新的集合中,然后遍历集合,把结果输出,这样运算次数就是3N了:其实可以在第二次循环的时候判

[LeetCode] NO. 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]. [题目解析] 这是一个很简单的求交集的问题,可以用Set来解决.如下. public static int[] intersection(int[] nums1, int[] nums2){ Set<Integer> set =

349. Intersection of Two Arrays java solutions

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. Subscribe to see which companies asked

【leetcode】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. 我们可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的

349. Intersection of Two Arrays (Easy)

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. 题意:保证列表中每个元素都是唯一的:利用python中列表求交集的方法: 思路

【一天一道LeetCode】#349. Intersection of Two Arrays

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