找到两个数组中的公共元素(2956)——哈希表或数组

给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们分别含有 n 和 m 个元素。请你计算以下两个数值:

  • answer1:使得 nums1[i] 在 nums2 中出现的下标 i 的数量。
  • answer2:使得 nums2[i] 在 nums1 中出现的下标 i 的数量。

返回 [answer1, answer2]

示例 1:

输入:nums1 = [2,3,2], nums2 = [1,2]

输出:[2,1]

解释:

示例 2:

输入:nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]

输出:[3,4]

解释:

nums1 中下标在 1,2,3 的元素在 nums2 中也存在。所以 answer1 为 3。

nums2 中下标在 0,1,3,4 的元素在 nums1 中也存在。所以 answer2 为 4。

示例 3:

输入:nums1 = [3,4,2,3], nums2 = [1,5]

输出:[0,0]

解释:

nums1 和 nums2 中没有相同的数字,所以答案是 [0,0]。

提示:

  • n == nums1.length
  • m == nums2.length
  • 1 <= n, m <= 100
  • 1 <= nums1[i], nums2[i] <= 100

问题简要描述:返回两个数组中的公共元素 

Java

class Solution {
    public int[] findIntersectionValues(int[] nums1, int[] nums2) {
        int[] s1 = new int[101];
        int[] s2 = new int[101];
        for (int x : nums1) {
            s1[x] = 1;
        }
        for (int x : nums2) {
            s2[x] = 1;
        }
        int[] ans = new int[2];
        for (int x : nums1) {
            ans[0] += s2[x];
        }
        for (int x : nums2) {
            ans[1] += s1[x];
        }
        return ans;
    }
}

 Python3

class Solution:
    def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
        s1, s2 = set(nums1), set(nums2)
        return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]        

TypeScript

function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
    let s1 = Array(101).fill(0);
    let s2 = Array(101).fill(0);
    for (const x of nums1) {
        s1[x] = 1;
    }
    for (const x of nums2) {
        s2[x] = 1;
    }
    let ans = Array(2).fill(0);
    for (const x of nums1) {
        ans[0] += s2[x];
    }
    for (const x of nums2) {
        ans[1] += s1[x];
    }
    return ans;    
};

C++

class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
		int s1[101]{};
		int s2[102]{};
		for (int& x : nums1) {
			s1[x] = 1;
		}
		for (int& x : nums2) {
			s2[x] = 1;
		}
		vector<int> ans(2);
		for (int& x : nums1) {
			ans[0] += s2[x];
		}
		for (int& x : nums2) {
			ans[1] += s1[x];
		}
		return ans;    
    }
};

Go

func findIntersectionValues(nums1 []int, nums2 []int) []int {
	s1 := [101]int{}
	s2 := [101]int{}
	for _, v := range nums1 {
		s1[v] = 1
	}
	for _, v := range nums2 {
		s2[v] = 1
	}
	ans := make([]int, 2)
	for _, v := range nums1 {
		ans[0] += s2[v]
	}
	for _, v := range nums2 {
		ans[1] += s1[v]
	}
	return ans
}

相关推荐

  1. LeetCode 2956.数组公共元素

    2024-07-21 16:18:02       18 阅读
  2. 力扣每日一题:2956. 数组公共元素

    2024-07-21 16:18:02       21 阅读
  3. 力扣刷题之2956.数组公共元素

    2024-07-21 16:18:02       18 阅读
  4. LeetCode 2657.数组前缀公共数组

    2024-07-21 16:18:02       36 阅读
  5. LeetCode-41. 缺失第一正数【数组

    2024-07-21 16:18:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 16:18:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 16:18:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 16:18:02       45 阅读
  4. Python语言-面向对象

    2024-07-21 16:18:02       55 阅读

热门阅读

  1. 牛客暑假训练2 C.Red Walking on Grid

    2024-07-21 16:18:02       17 阅读
  2. Python之后端Django(六)

    2024-07-21 16:18:02       13 阅读
  3. blender和3dmax和maya和c4d比较

    2024-07-21 16:18:02       16 阅读
  4. 数据结构第33节 在不同领域的应用

    2024-07-21 16:18:02       12 阅读
  5. 【软考】UML中的关联关系

    2024-07-21 16:18:02       20 阅读
  6. firefly rk3288 ubuntu23.10 网卡名为end0 改为eth0

    2024-07-21 16:18:02       15 阅读
  7. C++狼人杀游戏(真的能运行!!!)

    2024-07-21 16:18:02       14 阅读
  8. 跨平台游戏引擎 Axmol-2.1.4 发布

    2024-07-21 16:18:02       21 阅读
  9. 使用 hutool工具实现导入导出功能。

    2024-07-21 16:18:02       20 阅读
  10. https证书申请

    2024-07-21 16:18:02       17 阅读
  11. feign 报错 Connection reset executing POST

    2024-07-21 16:18:02       14 阅读
  12. Python 热门面试题(七)

    2024-07-21 16:18:02       15 阅读