LeetCode 349: 两个数组的交集(Intersection of Two Arrays)解法汇总

本文提供了一种解决LeetCode中两数组交集问题的有效算法,通过使用unordered_set降低时间复杂度,展示了C++、Java和Python三种语言的实现方式,并介绍了如何利用内置方法简化代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


更多LeetCode题解

我的解法

class Solution {
public:
	vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
		vector<int> res = {};
		for (int i = 0; i < nums1.size(); i++) {
			if (nums2.end() != find(nums2.begin(), nums2.end(), nums1[i]) && res.end() == find(res.begin(), res.end(), nums1[i])) {
				res.push_back(nums1[i]);
			}
		}
		return res;
	}
};

降低时间复杂度

此题稍微改进一点的解法就是使用unordered_set来降低时间复杂度,因为set的读取是 O ( 1 ) O(1) O(1)

class Solution {
public:
	vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
		unordered_set<int> set1, set2;
		for (int num : nums1) {
			set1.insert(num);
		}
		for (int num : nums2) {
			set2.insert(num);
		}
		if (set1.size() < set2.size()) return set_intersection(set1, set2);
		else return set_intersection(set2, set1);
	}
	vector<int> set_intersection(unordered_set<int>& set1, unordered_set<int>& set2) {
		vector<int> res;
		for (int num : set1) {
			if (set2.count(num)) {
				res.push_back(num);
			}
		}
		return res;
	}
};

其他语言参照

java

class Solution {
  public int[] set_intersection(HashSet<Integer> set1, HashSet<Integer> set2) {
    int [] output = new int[set1.size()];
    int idx = 0;
    for (Integer s : set1)
      if (set2.contains(s)) output[idx++] = s;

    return Arrays.copyOf(output, idx);
  }

  public int[] intersection(int[] nums1, int[] nums2) {
    HashSet<Integer> set1 = new HashSet<Integer>();
    for (Integer n : nums1) set1.add(n);
    HashSet<Integer> set2 = new HashSet<Integer>();
    for (Integer n : nums2) set2.add(n);

    if (set1.size() < set2.size()) return set_intersection(set1, set2);
    else return set_intersection(set2, set1);
  }
}

python

class Solution:
    def set_intersection(self, set1, set2):
        return [x for x in set1 if x in set2]
        
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """  
        set1 = set(nums1)
        set2 = set(nums2)
        
        if len(set1) < len(set2):
            return self.set_intersection(set1, set2)
        else:
            return self.set_intersection(set2, set1)

此外,java和python都有内置的方法来简化代码,分别如下

class Solution {
  public int[] intersection(int[] nums1, int[] nums2) {
    HashSet<Integer> set1 = new HashSet<Integer>();
    for (Integer n : nums1) set1.add(n);
    HashSet<Integer> set2 = new HashSet<Integer>();
    for (Integer n : nums2) set2.add(n);

    set1.retainAll(set2);

    int [] output = new int[set1.size()];
    int idx = 0;
    for (int s : set1) output[idx++] = s;
    return output;
  }
}
class Solution:
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """  
        set1 = set(nums1)
        set2 = set(nums2)
        return list(set2 & set1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值