------------一方面用于自己记录,一方面给小伙伴们一个参考-----------如有错误,恳请指正-------------
考试时间60分钟,三个在线编程。
题目描述:1.给定两个整形数组,求差集;需要补全的函数传入的参数是两个数组及对应的数组长度;
思考:经典题目,衍生出求并集,交集,以下给出两种思路,并分别分析复杂度:
思路一:将array1分为三段(建议做的时候画图),分别为:已经比较过的(即在array1中也在array2中),还未比较的,已经比较的(在array1,不在array2中);定义变量i为array1中未比较的第一个位置,end为未比较的最后一个位置,那么上述对应的三段分别为[0-i],[i-end],[end-length1];从i开始遍历array2,有两种情况:1.array2中找到与array[i]相等的array2[j],交换array2[i]与array[j],i++;2,array2中没有与array1[i]相等的,交换array1[i],array1[--end];
#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void help(int array1[],int length1,int array2[],int length2)
{
bool flag = false;
int end = length1;
for (int i = 0; i < end; ){
flag = false;
for (int j = i; j < length2; j++)
{
if (array1[i] == array2[j])
{
swap(array2[i], array2[j]);
flag = true;
break;
}
}
if (flag)
{
i++;
}
else
{
swap(array1[i], array1[--end]);
}
}
cout << "only in array1:" << " ";
for (int i = end; i < length1; i++)
{
cout << array1[i] << " ";
}
cout << endl;
cout << "only in array2:" << " ";
for (int i = end; i < length2; i++)
{
cout << array2[i] << " ";
}
cout << endl;
cout << "same in array1 & array2:" << " ";
for (int i = 0; i < end; i++)
{
cout << array1[i] << " ";
}
cout << endl;
cout << "all of array1 & array2:" << " ";
for (int i = 0; i < length1; i++)
{
cout << array1[i] << " ";
}
for (int i = end; i < length2; i++)
{
cout << array2[i] << " ";
}
cout << endl;
}
void main()
{
int array1[] = {1,2,3,4};
int array2[] = {2,3,4,5};
int length1 = sizeof(array1) / sizeof(array1[0]);
int length2 = sizeof(array2) / sizeof(array2[0]);
help(array1, length1, array2, length2);
system("pause");
}
运行结果:
复杂分析:空间复杂度:O(1);最好情况下两数组完全相等,时间复杂度:O(n),最坏情况下两数组完全不等,时间复杂度:O(n^2);
思路二:其实刚拿到这个题目第一思路就是先排序,求两个数组的交集,再求交集在原数组中的补集就是差集。
#include<iostream>
#include<algorithm>
using namespace std;
//求C在A中的补集
void buji(int A[], int lengthA, int C[], int lengthC)
{
int i = 0, j = 0;
while (i < lengthA && j < lengthC)
{
if (A[i] < C[j])
{
cout << A[i] << " ";
i++;
}
else
{
i = i + lengthC;
break;
}
}
while (i < lengthA)
{
cout << A[i++] << " ";
}
}
//求A,B的交集
void jiaoji(int A[],int B[],int lengthA,int lengthB)
{
//先排序
sort(A, A + lengthA);
sort(B, B + lengthB);
int *C=new int(lengthA+lengthB);
int i = 0, j = 0,k=0;
while (i < lengthA && j < lengthB)
{
if (A[i] == B[j])
{
C[k++] = A[i];
i++;
j++;
}
if (A[i]>B[j])
{
j++;
}
if (A[i]<B[j])
{
i++;
}
}
cout << "交集:" << " ";
for (i=0; i < k; i++)
{
cout << C[i] << " ";
}
cout << endl;
cout << "在array1不在array2中:" << " ";
buji(A, lengthA, C, k);
cout << endl;
cout << "在array2不在array1中:" << " ";
buji(B, lengthB, C, k);
cout << endl;
cout << "并集:" << " ";
buji(A, lengthA, C, k);
for (i = 0; i < lengthB; i++)
{
cout << B[i] << " ";
}
cout << endl;
}
void main()
{
int array1[] = { 1, 2, 3, 4 };
int array2[] = { 2, 3, 4, 5 };
int length1 = sizeof(array1) / sizeof(array1[0]);
int length2 = sizeof(array2) / sizeof(array2[0]);
//两个整形数组的交集
jiaoji(array1,array2,length1,length2);
system("pause");
}
运行结果:
复杂分析:空间复杂度:O(n);时间复杂度:O(n);