找出数组{1,2,3,4,...N-1}中出现的唯一重复数


异或法:

/*
找出数组{1,2,3,4,...N-1}中出现的唯一重复数 

*/

#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int xor_findDup(int* a,int count)
{
	int i = 0;
	int temp = 0;
	for(i=0;i<count;i++)
	{
		temp =  temp ^ a[i];	
	}
	for(i=1;i<count;i++)
	{
		temp = temp ^ i;	
	}
	return temp;	
} 

int main(int argc, char *argv[]) 
{
	int a[]={1,3,2,4,2};
	int length = sizeof(a)/sizeof(int);
	
	cout << "Duplication is : " << xor_findDup(a,length) << endl;
	return 0;
}

2.位图法

/*
找出数组{1,2,3,4,...N-1}中出现的唯一重复数 

*/

#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int xor_findDup(int* a,int count)
{
	int i = 0;
	int temp = 0;
	int* tempArray = (int*) malloc(sizeof(int)*(count));
	for(i=0;i<count;i++)
	{
		tempArray[i] = 0;	
	} 
	for(i=0;i<count;i++)
	{
		if(tempArray[a[i]]==0)
		{
			tempArray[a[i]] = 1;	
		}	
		else
		{
			temp = a[i];
			return a[i];	
		}
	}
	return temp;	
} 

int main(int argc, char *argv[]) 
{
	int a[]={1,3,1,4,2};
	int length = sizeof(a)/sizeof(int);
	
	cout << "Duplication is : " << xor_findDup(a,length) << endl;
	return 0;
}

上面的都有额外的开销


1.Hash法

/*
找出数组{1,2,3,4,...N-1}中出现的唯一重复数 

*/

#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int xor_findDup(int* a,int count)
{
	int i = 0;
	for(i=0;i<count;i++)
	{
		if(a[i]>0)
		{
			if(a[a[i]]>0)
			{
				a[a[i]] = - a[a[i]];	
			}
			else
			{
				return a[i];	
			}	
		}	
		else
		{
			if(a[-a[i]] > 0)
			{
				a[-a[i]] = -a[-a[i]];	
			}	
			else
			{
				return -a[i];	
			}
		}
	}
//	return 0;
} 

int main(int argc, char *argv[]) 
{
	int a[]={4,1,2,1,3};
	int length = sizeof(a)/sizeof(int);
	
	cout << "Duplication is : " << xor_findDup(a,length) << endl;
	return 0;
}

2.循环链表法

/*
找出数组{1,2,3,4,...N-1}中出现的唯一重复数 

*/

#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int xor_findDup(int* a,int count)
{
	int i = 0;	
	int x = 0,y = 0;
	do
	{
		x = a[a[x]]; //一次走两步 
		y = a[y];	 //一次走一步 
		cout << x << "->" << a[x] << "->"<<a[a[x]] << " ; " << y << "-" << a[y] << endl;
	}while(x != y);  //找到环中的一个点
	cout << endl;
	cout << x << endl;
	do
	{
		x = a[x];   //一次走一步 
		y = a[y];	 //一次走一步 
		cout << x << "->" << a[x] << " ; " << y << "->" << a[y] << endl;
	} while(x != y);  //找到入口点 
	return x;
//	return 0;
} 

int main(int argc, char *argv[]) 
{
	int a[]={4,1,2,3,4};
	int length = sizeof(a)/sizeof(int);
	
	cout << "Duplication is : " << xor_findDup(a,length) << endl;
	return 0;
}

不能从元素0开始,防止死循环!!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为您演示如何从多个数组去除复元素,取得唯一的n位数所有组合。 假设我们有三个数组,分别是`[1, 2, 3]`、`[2, 3, 4]`和`[3, 4, 5]`。我们希望从这三个数组取得唯一的两位数所有组合,去除复元素。 首先,我们可以将这三个数组合并成一个大数组,然后使用Set数据结构去除复元素。接着,我们可以使用递归函数来找出所有唯一的n位数组合。 下面是示例代码: ``` function getUniqueCombinations(arr, n) { // 去除复元素 const set = new Set(arr); const uniqueArr = Array.from(set); const result = []; function combine(selected, index) { if (selected.length === n) { result.push(selected.slice()); return; } for (let i = index; i < uniqueArr.length; i++) { selected.push(uniqueArr[i]); combine(selected, i + 1); selected.pop(); } } combine([], 0); return result; } const arr1 = [1, 2, 3]; const arr2 = [2, 3, 4]; const arr3 = [3, 4, 5]; const mergedArr = [...arr1, ...arr2, ...arr3]; const combinations = getUniqueCombinations(mergedArr, 2); console.log(combinations); // [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]] ``` 在这个示例,我们首先将三个数组合并成一个大数组`mergedArr`,然后使用Set数据结构去除复元素。接着,我们使用递归函数`combine`来找出所有唯一的n位数组合,最后返回结果数组`result`。 运行上面的代码,将会得到所有唯一的两位数组合,结果如下: ``` [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]] ``` 如果您需要取得其他位数的唯一组合,只需要将函数调用的第二个参数`n`改为所需的位数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值