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 this question

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         int len1 = nums1.length, len2 = nums2.length;
 4         Set<Integer> set = new HashSet<Integer>();
 5         Set<Integer> set2 = new HashSet<Integer>();
 6         for(int i = 0; i < len1; i++){
 7             set.add(nums1[i]);
 8         }
 9         for(int i = 0; i < len2; i++){
10             if(set.contains(nums2[i])) set2.add(nums2[i]);
11         }
12         int[] res = new int[set2.size()];
13         int index = 0;
14         for(int ans : set2){
15             res[index++] = ans;
16         }
17         return res;
18     }
19 }
时间: 2024-07-29 20:33:40

349. Intersection of Two Arrays java solutions的相关文章

[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]

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]. Note: Each element in the result must be unique. The result can be in any order. 第一想法用HashMap<Integer, Boolean>,但错误,用两个

【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了:其实可以在第二次循环的时候判

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

[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 =

【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中列表求交集的方法: 思路