首页 > 试题广场 >

两个数组的交集

[编程题]两个数组的交集
  • 热度指数:2109 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定两个整数数组分别为nums1, nums2,找到它们的公共元素并按返回。
数据范围:


示例1

输入

[1,2 ],[2,2,2,2]

输出

[2]

说明

两个数组的公共元素只有2 
示例2

输入

[1,2,3],[8,2,2,3,8]

输出

[2,3]

说明

两个数组的公共元素为2和3,返回[3,2]也是一个正确的答案 
package main

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param nums1 int整型一维数组 
 * @param nums2 int整型一维数组 
 * @return int整型一维数组
*/
func intersection( nums1 []int ,  nums2 []int ) []int {
    cnt1,cnt2:=map[int]int{},map[int]int{}
    for _,x:=range nums1{
        cnt1[x]++
    }
    for _,x:=range nums2{
        cnt2[x]++
    }
    ans:=[]int{}
    for k,_:=range cnt1{
        if _,ok:=cnt2[k];ok{
            ans=append(ans,k)
        }
    }
    return ans
}

func min(a,b int)int{
    if a>b{
        return b
    }
    return a
}

发表于 2023-03-09 08:19:21 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param nums1 int整型一维数组 
 * @param nums1Len int nums1数组长度
 * @param nums2 int整型一维数组 
 * @param nums2Len int nums2数组长度
 * @return int整型一维数组
 * @return int* returnSize 返回数组行数
 */
int* intersection(int* nums1, int nums1Len, int* nums2, int nums2Len, int* returnSize ) {
    // write code here
    int* hash=(int *)calloc(1000,sizeof(int));
    int* ans=(int *)malloc(1000*sizeof(int));
    for(int i=0;i<nums1Len;i++){
        hash[nums1[i]-1]=1;
    }
    int count=0;
    for(int j=0;j<nums2Len;j++){
        if(hash[nums2[j]-1]==1){
            hash[nums2[j]-1]+=1;
            if(hash[nums2[j]-1]==2){
                    ans[count++]=nums2[j];
        }
        }
    }
    *returnSize=count;
    return ans;

    
}

发表于 2022-12-24 07:52:44 回复(0)
主要考虑重复项
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        ArrayList<Integer> result = new ArrayList<Integer>();

        for (int i = 0; i < nums1.size(); i++) {
            if (nums2.contains(nums1.get(i))) {
                result.add(nums1.get(i));
                while(nums2.remove(nums1.get(i)));
            }
        }

        return result;
    }

发表于 2022-09-21 21:55:40 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums1 int整型vector 
     * @param nums2 int整型vector 
     * @return int整型vector
     */
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        // write code here
        unordered_set<int> arr1, arr2;
        for(auto& au:nums1)
        {
            arr1.insert(au);
        }
        for(auto& au:nums2)
        {
            arr2.insert(au);
        }
        return GetIntersection(arr1, arr2);
    }
    vector<int> GetIntersection(unordered_set<int>& arr1, unordered_set<int>& arr2)
    {
        if(arr1.size()>arr2.size())
            return GetIntersection(arr2, arr1);
        vector<int> arr3;
        for(auto& au:arr1)
        {
            if(arr2.count(au))
                arr3.push_back(au);
        }
        return arr3;
    }
};

发表于 2022-09-20 17:56:13 回复(0)
class Solution:
    def intersection(self , nums1: List[int], nums2: List[int]) -> List[int]:
        # write code here
        return list(set(nums1) & set(nums2))

发表于 2022-07-09 00:15:24 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums1 int整型ArrayList 
     * @param nums2 int整型ArrayList 
     * @return int整型ArrayList
     */
     public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        HashSet<Integer> set = new HashSet<>();
        HashSet<Integer> temp = new HashSet<>();
        ArrayList<Integer> res = new ArrayList<>();

        for (int i = 0;  i < nums1.size(); i++) {
            set.add(nums1.get(i));
        }

        for (int i = 0; i < nums2.size(); i++) {
            if (set.contains(nums2.get(i))) {
                temp.add(nums2.get(i));
            }
        }

        for (int t : temp) {
            res.add(t);
        }

        return res;
    }
}

发表于 2022-06-09 10:02:29 回复(0)
class Solution:
    def intersection(self , nums1: List[int], nums2: List[int]) -> List[int]:
        # write code here
        return list(set(nums1)&set(nums2))

发表于 2022-04-22 15:20:04 回复(0)

问题信息

难度:
7条回答 2668浏览

热门推荐

通过挑战的用户

查看代码