两个数组的交集(给定两个数组,编写一个函数来计算它们的交集。)

计算两个数组交集的方法
博客介绍了计算两个数组交集的问题,给出示例输入和输出。解题思路是先对两个数组排序,使用Arrays.sort()方法,再用一个数组的数在另一个数组中找相同数,将相同数存入要返回的数组。

给定两个数组,编写一个函数来计算它们的交集。

示例 1:

输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2]
示例 2:

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [9,4]

解题思路:先将两个给定的数组排序(用Arrays.sort()),然后用一个数组中的数去另一个数组中找相同的数,找到后存入要返回的数组中。具体操作的话看代码。

import java.util.Arrays;

public class test0322 {
	public static void main(String[] args) {
		Solution S = new Solution();
		int[] nums1 = new int[] { 4, 9, 5};
		int[] nums2 = new int[] { 9, 4, 9, 8};
		int[] a = S.intersection(nums1, nums2);
		for(int i : a) {
			System.out.print(i + " ");
		}
	}
}
class Solution{
	 public int[] intersection(int[] nums1, int[] nums2) {
		 Arrays.sort(nums1);
		 Arrays.sort(nums2);
		 int[] num = new int[] {};
		 for(int i = 0; i < nums1.length; i++) {
			 if(i == nums1.length - 1 || nums1[i] < nums1[i+1]) {
				 //只有后一位的数大于前一位的才可以或者是最后一位数
				 for(int j = 0; j < nums2.length; j++) {
					 if(nums1[i] == nums2[j]) {
						 num = Arrays.copyOf(num, num.length+1);//数组扩容
						 num[num.length-1] = nums1[i];
						 break;
					 }
				 }
			 }
		 }
		 return num;
	 }
}

 

要使用查找表实现给定两个数组交集,可以使用哈希表来记录数组中的元素,并比较两个数组中的元素是否存在于哈希表中。以下是使用C语言实现的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义哈希表结点结构 struct HashNode { int key; struct HashNode* next; }; // 初始化哈希表 struct HashNode** initHashTable(int size) { struct HashNode** hashTable = (struct HashNode**)malloc(size * sizeof(struct HashNode*)); for (int i = 0; i < size; i++) { hashTable[i] = NULL; } return hashTable; } // 哈希函数 int hash(int key, int size) { return abs(key) % size; } // 插入哈希表 void insert(struct HashNode** hashTable, int key, int size) { int index = hash(key, size); struct HashNode* newNode = (struct HashNode*)malloc(sizeof(struct HashNode)); newNode->key = key; newNode->next = NULL; if (hashTable[index] == NULL) { hashTable[index] = newNode; } else { struct HashNode* currentNode = hashTable[index]; while (currentNode->next != NULL) { currentNode = currentNode->next; } currentNode->next = newNode; } } // 判断元素是否存在于哈希表中 int contains(struct HashNode** hashTable, int key, int size) { int index = hash(key, size); struct HashNode* currentNode = hashTable[index]; while (currentNode != NULL) { if (currentNode->key == key) { return 1; } currentNode = currentNode->next; } return 0; } // 释放哈希表内存 void freeHashTable(struct HashNode** hashTable, int size) { for (int i = 0; i < size; i++) { struct HashNode* currentNode = hashTable[i]; while (currentNode != NULL) { struct HashNode* temp = currentNode; currentNode = currentNode->next; free(temp); } } free(hashTable); } // 计算数组交集 int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) { // 确定较小的数组作为哈希表大小 int size = nums1Size > nums2Size ? nums2Size : nums1Size; // 初始化两个哈希表 struct HashNode** hashTable1 = initHashTable(size); struct HashNode** hashTable2 = initHashTable(size); // 插入第一个数组的元素到哈希表1 for (int i = 0; i < nums1Size; i++) { insert(hashTable1, nums1[i], size); } // 插入第二个数组的元素到哈希表2,并检查是否存在于哈希表1中 int* result = (int*)malloc(size * sizeof(int)); int resultIndex = 0; for (int i = 0; i < nums2Size; i++) { if (contains(hashTable1, nums2[i], size) && !contains(hashTable2, nums2[i], size)) { result[resultIndex++] = nums2[i]; insert(hashTable2, nums2[i], size); } } // 更新返回的数组长度 *returnSize = resultIndex; // 释放哈希表内存 freeHashTable(hashTable1, size); freeHashTable(hashTable2, size); return result; } int main() { int nums1[] = {1, 2, 2, 1}; int nums1Size = sizeof(nums1) / sizeof(nums1[0]); int nums2[] = {2, 2}; int nums2Size = sizeof(nums2) / sizeof(nums2[0]); int size; int* result = intersect(nums1, nums1Size, nums2, nums2Size, &size); printf("交集:"); for (int i = 0; i < size; i++) { printf("%d ", result[i]); } free(result); return 0; } ``` 以上是一个用C语言实现的示例代码,使用了哈希表来计算两个数组交集。您可以根据自己的需进行修改和使用。希望对您有帮助!如有任何疑问,随时追问。
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值