校园招聘:小米2013 最新笔试题

题目:一个数组里,除了三个数是唯一出现的,其余的都出现偶数个,找出这三个数中的任一个。比如数组元素为【1, 2,4,5,6,4,2】,只有1,5,6这三个数字是唯一出现的,我们只需要输出1,5,6中的一个就行。

下面是我的解法,找到三个数字一个数的第一个bit位(这里是从右到左算)和其它二个不一样的数就行

如1,5,6的二进制分别为0001,0101,0110。因为6的的第一位为0,而其他的为1,用我的程序第一个输出的就是6了。程序如下:

  1. #include <stdio.h>   
  2. //得到第i位的二进制    
  3. #define isON(n, i) ((n) & 1 << (i))   
  4.   
  5. // Author: 397090770   
  6. // E-mail: wyphao.2007@163.com   
  7. // 转载请注明出处    
  8.   
  9. void findTheSingleNumber(int *arr, int size){  
  10.     int tempA = 0, tempB = 0;  
  11.     int countA = 0, countB = 0;  
  12.     int i = 0, j = 0;  
  13.       
  14.     for(i = 0; i < 31; i++){ //32位平台上,int只有32位    
  15.         tempA = tempB = countA = countB = 0;  
  16.         for(j = 0; j < size; j++){//遍历数组    
  17.             if(isON(arr[j], i)){  
  18.                 tempA ^= arr[j];  
  19.                 countA++;  
  20.             }else{  
  21.                 tempB ^= arr[j];  
  22.                 countB++;     
  23.             }  
  24.         }  
  25.           
  26.         if(countA & 0x1){//奇数    
  27.             if(0 == tempB){   
  28.                 continue;   
  29.             }else{  
  30.                 printf("%d\n", tempA);//肯定是不同的数字    
  31.                 break;   
  32.             }  
  33.         }else{  
  34.             if(0 == tempA){  
  35.                 continue;  
  36.             }else{  
  37.                 printf("%d\n", tempB);  
  38.                 break;   
  39.             }  
  40.         }  
  41.     }  
  42.       
  43. }  
  44.   
  45. int main(){  
  46.     int arr[] = {  
  47.         /*1, 3, -9, 2, 1, 2, -10*/  
  48.         1, 2,4,5,6,4,2  
  49.     };  
  50.       
  51.     findTheSingleNumber(arr, sizeof(arr) / sizeof(arr[0]));  
  52.     return 0;  
  53. }  
#include <stdio.h>
//得到第i位的二进制 
#define isON(n, i) ((n) & 1 << (i))

// Author: 397090770
// E-mail: wyphao.2007@163.com
// 转载请注明出处 

void findTheSingleNumber(int *arr, int size){
	int tempA = 0, tempB = 0;
	int countA = 0, countB = 0;
	int i = 0, j = 0;
	
	for(i = 0; i < 31; i++){ //32位平台上,int只有32位 
		tempA = tempB = countA = countB = 0;
		for(j = 0; j < size; j++){//遍历数组 
			if(isON(arr[j], i)){
				tempA ^= arr[j];
				countA++;
			}else{
				tempB ^= arr[j];
				countB++;	
			}
		}
		
		if(countA & 0x1){//奇数 
			if(0 == tempB){ 
				continue; 
			}else{
				printf("%d\n", tempA);//肯定是不同的数字 
				break; 
			}
		}else{
			if(0 == tempA){
				continue;
			}else{
				printf("%d\n", tempB);
				break; 
			}
		}
	}
	
}

int main(){
	int arr[] = {
		/*1, 3, -9, 2, 1, 2, -10*/
		1, 2,4,5,6,4,2
	};
	
	findTheSingleNumber(arr, sizeof(arr) / sizeof(arr[0]));
	return 0;
}
时间复杂度为O(N)。输出为  6。(如果要全部输出不同的数,方法和上面的一样)

哪位大牛还有别的解法吗?


另附:第一大题为求最大子序列。上面那题为第二大题。最后一道题:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iot-genius

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值