找出数组中两个只出现一次的数字--分组异或

本文介绍了一种高效算法,用于在一个整型数组中找出仅出现一次的两个数字,其余数字均出现两次。采用异或运算实现时间复杂度O(n)与空间复杂度O(1)的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)

解题思路:

这个题目的突破口在哪里?题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现依次的数字,因为那些出现两次的数字全部在异或中抵消掉了。

有了上面简单问题的解决方案之后,我们回到原始的问题。如果能够把原数组分为两个子数组。在每个子数组中,包含一个只出现一次的数字,而其他数字都出现两次。如果能够这样拆分原数组,按照前面的办法就是分别求出这两个只出现一次的数字了。

我们还是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。由于这两个数字肯定不一样,那么这个异或结果肯定不为0,也就是说在这个结果数字的二进制表示中至少就有一位为1。我们在结果数字中找到第一个为1的位的位置,记为第N位。现在我们以第N位是不是1为标准把原数组中的数字分成两个子数组,第一个子数组中每个数字的第N位都为1,而第二个子数组的每个数字的第N位都为0

现在我们已经把原数组分成了两个子数组,每个子数组都包含一个只出现一次的数字,而其他数字都出现了两次。

代码如下:

/**
  * @param args
  */
 ///////////////////////////////////////////////////////////////////////
 // Find the index of first bit which is 1 in num (assuming not 0)
 ///////////////////////////////////////////////////////////////////////
 static int num1=0,num2=0;
 public static int FindFirstBitIs1(int num) {
  int indexBit = 0;
  while (((num & 1) == 0) && (indexBit < 32)) {
   num = num >> 1;
   ++indexBit;
  }
  return indexBit;
 }
 ///////////////////////////////////////////////////////////////////////
 // Is the indexBit bit of num 1?
 ///////////////////////////////////////////////////////////////////////
 public static boolean IsBit1(int num, int indexBit)
 {
       num = num >> indexBit;
       if((num&1)==1)
        return true;
       else
        return false;
 }
 
 ///////////////////////////////////////////////////////////////////////
 // Find two numbers which only appear once in an array
 // Input: data - an array contains two number appearing exactly once,
 //                while others appearing exactly twice
 //         length - the length of data
 // Output: num1 - the first number appearing once in data
 //          num2 - the second number appearing once in data
 ///////////////////////////////////////////////////////////////////////
 public static void FindNumsAppearOnce(int data[], int length)
 {
       if (length < 2)
             return;
 
       // get num1 ^ num2
       int resultExclusiveOR = 0;
       for (int i = 0; i < length; ++ i)
             resultExclusiveOR ^= data[i];
 
       // get index of the first bit, which is 1 in resultExclusiveOR
       int indexOf1 = FindFirstBitIs1(resultExclusiveOR);
 
       //num1 = num2 = 0;
       for (int j = 0; j < length; ++ j)
       {
             // divide the numbers in data into two groups,
             // the indexOf1 bit of numbers in the first group is 1,
             // while in the second group is 0
             if(IsBit1(data[j], indexOf1))
                   num1 ^= data[j];
             else
                   num2 ^= data[j];
       }
 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值