zoukankan      html  css  js  c++  java
  • 面试题40:数组中只出现一次的数字

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

     1 void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
     2         if(data.size()<=1)
     3             return;
     4         int resultXOR = 0;
     5         for(int i=0;i<=data.size()-1;i++)
     6             resultXOR^=data[i];
     7         int indexOf1 = FindFirstBits1(resultXOR);
     8         *num1=*num2=0;
     9         for(int j=0;j<=data.size()-1;j++){
    10             if(isBit1(data[j],indexOf1))
    11                 *num1^=data[j];
    12             else
    13                 *num2^=data[j];
    14         }
    15     }
    16     
    17     int FindFirstBits1(int num){
    18         int count=0;
    19         while((0x1&num)==0){
    20             num=num>>1;
    21             count++;
    22         }
    23         return count;
    24         
    25     }
    26     
    27     bool isBit1(int num,int indexBit){
    28         num=num>>indexBit;
    29         return (num&0x1);
    30     }
  • 相关阅读:
    叉积
    Linux IO模型
    uva10201-dp
    如何在Java内使用Protobuf
    uva10651-记忆化搜索
    ZK的几个常用方式
    uva10304-最优二叉搜索树
    uva590-DP-Always on the run
    Git神操作
    在容器内运行JVM时内存的问题
  • 原文地址:https://www.cnblogs.com/yangrenzhi/p/5849724.html
Copyright © 2011-2022 走看看