【白话经典算法系列之十二】数组中只出现1次的两个数字(百度面试题)


微博http://weibo.com/MoreWindows已开通,欢迎关注。

本系列文章地址:http://blog.csdn.net/MoreWindows/article/category/859207

首先来看题目要求:

在一个数组中除两个数字只出现1次外,其它数字都出现了2次, 要求尽快找出这两个数字。

    考虑下这个题目的简化版——数组中除一个数字只出现1次外,其它数字都成对出现,要求尽快找出这个数字。这个题目在之前的《位操作基础篇之位操作全面总结》中的“位操作趣味应用”中就已经给出解答了。根据异或运算的特点,直接异或一次就可以找出这个数字。

    现在数组中有两个数字只出现1次,直接异或一次只能得到这两个数字的异或结果,但光从这个结果肯定无法得到这个两个数字。因此我们来分析下简化版中“异或”解法的关键点,这个关键点也相当明显——数组只能有一个数字出现1

    设题目中这两个只出现1次的数字分别为AB,如果能将AB分开到二个数组中,那显然符合“异或”解法的关键点了。因此这个题目的关键点就是将AB分开到二个数组中由于AB肯定是不相等的,因此在二进制上必定有一位是不同的。根据这一位是0还是1可以将AB分开到A组和B组。而这个数组中其它数字要么就属于A组,要么就属于B组。再对A组和B组分别执行“异或”解法就可以得到AB了。而要判断AB在哪一位上不相同,只要根据A异或B的结果就可以知道了,这个结果在二进制上为1的位都说明AB在这一位上是不相同的。

    比如int a[] = {1, 1, 3, 5, 2, 2}

    整个数组异或的结果为3^5 0x0011 ^ 0x0101 = 0x0110

    对0x0110,第1位(由低向高,从0开始)就是1。因此整个数组根据第1位是0还是1分成两组。

    a[0] =1  0x000第一组

    a[1] =1  0x000第一组

    a[2] =3  0x001第二组

    a[3] =5  0x010第一组

    a[4] =2  0x001第二组

    a[5] =2  0x001第二组

    第一组有{1, 1, 5},第二组有{3, 2, 3},明显对这二组分别执行“异或”解法就可以得到53了。

    分析至些,相信代码不难写出,下面给出完整的源代码:

  1. // 百度面试题   
  2. //数组中除两个数字外,其它数字都出现了次。要求尽可能快的找出这两个数字   
  3. //By MoreWindows (http://blog.csdn.net/MoreWindows)   
  4. #include <stdio.h>   
  5. void FindTwoNotRepeatNumberInArray(int *a, int n, int *pN1, int *pN2)  
  6. {  
  7.     int i, j, temp;  
  8.       
  9.     //计算这两个数的异或结果   
  10.     temp = 0;  
  11.     for (i = 0; i < n; i++)  
  12.         temp ^= a[i];  
  13.       
  14.     // 找第一个为1的位   
  15.     for (j = 0; j < sizeof(int) * 8; j++)  
  16.         if (((temp >> j) & 1) == 1)  
  17.             break;  
  18.   
  19.     // 第j位为1,说明这两个数字在第j位上是不相同的   
  20.     // 由此分组即可   
  21.     *pN1 = 0, *pN2 = 0;  
  22.     for (i = 0; i < n; i++)  
  23.         if (((a[i] >> j) & 1) == 0)  
  24.             *pN1 ^= a[i];  
  25.         else  
  26.             *pN2 ^= a[i];  
  27. }  
  28. void PrintfArray(int a[], int n)  
  29. {  
  30.     for (int i = 0; i < n; i++)  
  31.         printf("%d ", a[i]);  
  32.     putchar('\n');  
  33. }  
  34. int main()  
  35. {  
  36.     printf("    白话经典算法系列之十二数组中不重复的个数字(百度面试题) \n");        
  37.     printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");   
  38.   
  39.     const int MAXN = 10;  
  40.     //int a[MAXN] = {1, 2, 7, 5, 100,  100, 6, 1, 2, 5};   
  41.     int a[MAXN] = {1, 2, 3, 4, 1,  2, 3, 4, 0, 5};  
  42.   
  43.     printf("数组为: \n");  
  44.     PrintfArray(a, MAXN);  
  45.   
  46.     int nNotRepeatNumber1, nNotRepeatNumber2;  
  47.     FindTwoNotRepeatNumberInArray(a, MAXN, &nNotRepeatNumber1, &nNotRepeatNumber2);  
  48.     printf("两个不重复的数字分别为: %d %d\n", nNotRepeatNumber1, nNotRepeatNumber2);  
  49.     return 0;  
  50. }  
// 百度面试题
//数组中除两个数字外,其它数字都出现了次。要求尽可能快的找出这两个数字
//By MoreWindows (http://blog.csdn.net/MoreWindows)
#include <stdio.h>
void FindTwoNotRepeatNumberInArray(int *a, int n, int *pN1, int *pN2)
{
	int i, j, temp;
    
	//计算这两个数的异或结果
	temp = 0;
	for (i = 0; i < n; i++)
		temp ^= a[i];
	
	// 找第一个为1的位
	for (j = 0; j < sizeof(int) * 8; j++)
		if (((temp >> j) & 1) == 1)
			break;

	// 第j位为1,说明这两个数字在第j位上是不相同的
	// 由此分组即可
	*pN1 = 0, *pN2 = 0;
	for (i = 0; i < n; i++)
		if (((a[i] >> j) & 1) == 0)
			*pN1 ^= a[i];
		else
			*pN2 ^= a[i];
}
void PrintfArray(int a[], int n)
{
	for (int i = 0; i < n; i++)
		printf("%d ", a[i]);
	putchar('\n');
}
int main()
{
	printf("    白话经典算法系列之十二数组中不重复的个数字(百度面试题) \n");      
	printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); 

	const int MAXN = 10;
	//int a[MAXN] = {1, 2, 7, 5, 100,  100, 6, 1, 2, 5};
	int a[MAXN] = {1, 2, 3, 4, 1,  2, 3, 4, 0, 5};

	printf("数组为: \n");
	PrintfArray(a, MAXN);

	int nNotRepeatNumber1, nNotRepeatNumber2;
	FindTwoNotRepeatNumberInArray(a, MAXN, &nNotRepeatNumber1, &nNotRepeatNumber2);
	printf("两个不重复的数字分别为: %d %d\n", nNotRepeatNumber1, nNotRepeatNumber2);
	return 0;
}

运行结果如下所示:

 

百度面试在算法这一环节上一般会出多道算法题,下次再整理几篇,欢迎大家继续参阅。

 

白话经典算法系列文章地址:

http://blog.csdn.net/MoreWindows/article/category/859207

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8214003

欢迎关注微博:http://weibo.com/MoreWindows








自己写的程序:

// 百度面试题   
//数组中除两个数字外,其它数字都出现了次。要求尽可能快的找出这两个数字 
#include <iostream>
using namespace std;
#include <bitset>
void solution(const int* a,const int n);

int main()
{
	const int MAX=8;
	
	 int  a[MAX]={1,2,3,3,2,1,7,15};
	solution(a,8);
	return 0;

}

void solution(const int* a,const int n)
{
	int temp=0;

	//计算两个不同的数异或的结果
	for (int i=0;i<n;i++)
		temp^=a[i];

	//找到两个数第一个不同的位
	int loca;
	for (int i=0;i<sizeof(int)*8;i++)
		if ( (temp>>i) &1==1){
			loca=i;
			break;
		}

	cout<<"loca: "<<loca<<endl;

	int number=1;

	number=number<<loca;
	
	cout<<"number: "<<number<<endl;
	int temp1=0,temp2=0; //计算数组1和2的两个临时变量
	for (int i=0;i<n;i++){
		if ((a[i]&number)==number){
			temp1^=a[i];
		}
		else{
		   temp2^=a[i];
		}
	}

	cout<<temp1<<"   "<<temp2<<endl;


}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值