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

给定两个数组,求数组的交集。输出结果中的元素唯一,输出数组可以无序。

public int[] intersection(int[] nums1, int[] nums2) {
        int j = 0;
        int[] temp = new int[nums2.length];
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        for (int i = 0; i < nums1.length; i++)
            list.add(nums1[i]);
        for (int i = 0; i < nums2.length; i++)
            if (list.contains(nums2[i]) && !list2.contains(nums2[i])) {
                list2.add(nums2[i]);
                temp[j++] = nums2[i];
            }
        int[] temp2 = new int[j];
        for (int i = 0; i < temp2.length; i++) {
            temp2[i] = temp[i];
        }
        return temp2;
    }

几个小技巧自己看代码哈,反正就AC了。

时间: 2024-10-02 08:02:38

[LeetCode]--349. Intersection of Two Arrays的相关文章

[LeetCode]--350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any or

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

[LeetCode] 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. 解题思路 首先用map存储数组1里出现的元素,然后检查这些元素是否也在数组2

[LeetCode]160.Intersection of Two Linked Lists

[题目] Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

JavaScript获取两个数组交集的方法

  本文实例讲述了JavaScript获取两个数组交集的方法.分享给大家供大家参考.具体如下: 这里传入的数组必须是已经排过序的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /* finds the intersection of * two arrays in a simple fashion. * * PARAMS * a - first array, must alre

JavaScript获取两个数组交集的方法_javascript技巧

本文实例讲述了JavaScript获取两个数组交集的方法.分享给大家供大家参考.具体如下: 这里传入的数组必须是已经排过序的 /* finds the intersection of * two arrays in a simple fashion. * * PARAMS * a - first array, must already be sorted * b - second array, must already be sorted * * NOTES * * Should have O(

LeetCode 4 Median of Two Sorted Arrays

翻译 有两个给定的排好序的数组nums1和nums2,其大小分别为m和n. 找出这两个已排序数组的中位数. 总运行时间的复杂度应该是O(log(m+n)). 原文 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n

【LeetCode从零单排】No.160 Intersection of Two Linked Lists

题目 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have n