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

    题目描述

    一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
     1 class Solution {
     2 public:
     3     
     4     unsigned int FindOne(int num){
     5         int indexBit = 0;
     6         while ((num & 1) == 0 && indexBit < 8 * sizeof(int)){
     7             num >>= 1;
     8             ++indexBit;
     9         }
    10         return indexBit;
    11     }
    12     
    13     bool IsBit1(int num, unsigned int indexBit){
    14         num >>= indexBit;
    15         return num & 1;
    16     }
    17     
    18     void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
    19         if (data.size() < 2)
    20             return;
    21         int result = 0;
    22         for (int j = 0; j < data.size(); j++){
    23             result ^= data[j];
    24         }
    25         int indexOf1 = FindOne(result);
    26         for (int j = 0; j < data.size(); j++){
    27             if (IsBit1(data[j], indexOf1)){
    28                 *num1 ^= data[j];
    29             }
    30             else
    31                 *num2 ^= data[j];
    32         }
    33     }
    34 };
  • 相关阅读:
    负数幅角的选取
    记一次py交易
    区间估计
    平方和
    正态总体 下常用结论
    每日一背
    乘积的期望
    java调用javascript
    Java Agent入门
    JavaPoet入门
  • 原文地址:https://www.cnblogs.com/wanderingzj/p/5358906.html
Copyright © 2011-2022 走看看