哈希(4) - 求两个链表的交集(intersection)以及并集(union)

给定两个链表,求它们的交集以及并集。用于输出的list中的元素顺序可不予考虑。

例子:

输入下面两个链表:

list1: 10->15->4->20

list2: 8->4->2->10

输出链表:

交集list: 4->10

并集list: 2->8->20->4->15->10

方法1 (简单方法)

可以参考链表系列中的"链表操作 - 求两个链表的交集(intersection)以及并集(union)"

方法2 (使用归并排序)

可以参考链表系列中的"链表操作 - 求两个链表的交集(intersection)以及并集(union)"

方法3 (使用哈希)

calculateUnion (list1, list2),求链表的并集的算法:

将result list初始化为NULL,然后创建一个空的哈希表。依次遍历这两个链表,对于其中的每个元素,在哈希表中进行查找。如果元素不在哈希表中,则将其插入到result list中,并插入到哈希表中。如果元素已经存在于哈希表中,则忽略它。

calculateIntersection (list1, list2),求链表的交集的算法:

将result list初始化为NULL,然后创建一个空的哈希表。 遍历list1, 对于其中的每个元素,将其插入到哈希表中。然后遍历list2, 对于其中的每个元素,在哈希表中进行查找。如果元素已经存在于哈希表中,则插入到result list中。如果元素不在哈希表中,则忽略它。

上面的这两个方法,即使list中存在重复的元素,也能处理。

时间复杂度:方法3的时间复杂度取决于所使用的哈希方法,以及输入链表中的元素分布。在实际的情况中,方法3可能比前面的两种方法更好。

代码如下:

#include<iostream>
#include<algorithm>
#include<map>
#include<list>

void calculateUnion(const std::list<int>& list1, const std::list<int>& list2, std::list<int>& outputResult)
{
	std::map< int, bool> tmp;

	for (auto iter : list1)
	{
		if (tmp.find(iter) == tmp.end())
			tmp.insert(std::make_pair(iter, true));
	}

	for (auto iter : list2)
	{
		if (tmp.find(iter) == tmp.end())
			tmp.insert(std::make_pair(iter, true));
	}

	//convert map to list
	for (auto iter : tmp)
	{
		if (iter.second)
			outputResult.push_back(iter.first);
	}

}

void calculateIntersection(const std::list<int>& list1, const std::list<int>& list2, std::list<int>& outputResult)
{
	std::map< int, bool> tmp;

	for (auto iter : list1)
	{
		tmp.insert(std::make_pair(iter, false));
	}

	for (auto iter : list2)
	{
		if (tmp.find(iter) != tmp.end())
			outputResult.push_back(iter);
	}
}

int main()
{
	//list1: 10->15->4->20
	//list2:  8->4->2->10
	std::list< int> list1, list2, result;
	list1.push_back(10);
	list1.push_back(15);
	list1.push_back(4);
	list1.push_back(20);

	list2.push_back(8);
	list2.push_back(4);
	list2.push_back(2);
	list2.push_back(10);

	calculateUnion(list1, list2, result);
	std::cout << "union result: \n";
	std::for_each(result.begin(), result.end(), [](const int& value){
		std::cout << value << " ";
	});

	result.clear();
	calculateIntersection(list1, list2, result);
	std::cout << "\nintersection result: \n";
	std::for_each(result.begin(), result.end(), [](const int& value){
		std::cout << value << " ";
	});
	std::cout << std::endl;

	return 0;
}

输出:

union result:

2 4 8 10 15 20

intersection result:

4 10

时间: 2024-08-01 12:33:31

哈希(4) - 求两个链表的交集(intersection)以及并集(union)的相关文章

[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists

标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后就开始比较节点的值是否相等,直到示例跑失败了才知道原因. 2.通用解法 public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA!=null && headB!=null){ ListNode

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 ord

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. Subscribe to see which companies asked

[LeetCode] 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 no i

求两个数组的交集

问题: 给你两个排序的数组,求两个数组的交集. 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5. 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以,每一次从B数组取值后,可以利用二分查找看是否在数组A里有B所对应的值,这样复杂度变成了O(N lg M). 这里,如果N 比 M 大,可以从A中取值,然后在B中判断是否有A

Java求两个List的交集

1 package demo; 2 3 import java.util.List; 4 5 public class Demo { 6 7 @SuppressWarnings("unchecked") 8 public static void main(String[] args) { 9 List array1=new ArrayList(); 10 array1.add("1");array1.add("2"); 11 List array

求两个集合的交集和并集C#

我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JiaoJi { class Program { static void Main(string[] args) { int [] arrA=new int[8]{1,2,3,4,5,6,7,8}; int [] arrB=new int[5]{4,5,

链表:判断链表是否带环 、求两个链表的相交结点

问题一:返回两个链表的相交结点1.先分别得到两个链表的长度2.得到长度差,3.先让长链表的头结点走(长度差)步.4.这时.短链表头结点还在原地,两者开始一起走,当得到两者val相等时,这个结点就是公共结点,即相遇结点. 问题二:判断链表是否带环1.定义两个快慢指针,快指针先走两步,慢指针再走一步.直到快慢指针当前结点相同. 如果快指针先为null,则表示没有环,返回null.2.如果带环,让起点和相遇点同时出发.同走一步,再判断相等与否,如果相等退出循坏 返回这个结点 ```public cla

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 a