码迷,mamicode.com
首页 > 其他好文 > 详细

【01】数组中只出现一次的数字

时间:2014-06-09 19:23:56      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:c   style   code   a   ext   color   

其他两次,一个一次/其他三次,一个一次/其他两次,两个一次
============================================
任何一个数字异或他自己都得零。
注意异或运算的初始化变量为0,因为0异或任何数字都得那个数字自身。

Single Number

1.一个整型数组中除了一个数字之外,其他数字都出现了两次 ,找出这个出现一次的数字。
 
解法一,异或是重复的消失,剩下的就是要找的数字。
 
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
 
int singleNumber(int A[], int n)
{
    if(n < 0)
    {
        return -1;
    }
    int result = 0;
    for(int i = 0; i < n; ++i)
    {
        result ^= A[i];/*1^1=0 0^0=0*/
    }
    return result;
}
 

Single Number II

2.一个整型数组中除了一个数字之外,其他数字都出现了三次 ,找出这个出现一次的数字。
 
解法一,用一个数组模拟三进制运算,但是耗费空间复杂度,超过3,就对3取模。
 
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
int singleNumber(int A[], int n)
{
    //一个整数的bit数,即整数字长
    const int W = sizeof(int) * 8;
    //表示在i位出现的1的个数
    int count[W];
    //注意这里乘以4不要忘记了
    memset(count, 0, W * 4);
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < W; ++j)
        {
            count[j] += (A[i] >> j) & 0x1;
            if(count[j] >= 3)
            {
                /*因为每次累加1*/
                count[j] = 0;
            }
        }
    }
    int result = 0;
    for(int i = 0; i < W; ++i)
    {
        result += (count[i] << i);
    }
    return result;
}
 
 
解法二,用位运算记录出现一次的,出现两次的,出现三次的,
然后每次迭代,把出现三次的从出现两次的和出现一次的当中去除。
异或
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
~1 = 0
~0 = 1
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
int singleNumber(int A[], int n)
{
    int ones = 0, twos = 0, threes = 0;
    for(int i = 0; i < n; i++)
    {
        //已经出现两次并且再次出现
        threes = twos & A[i];
        //曾经出现两次的或者曾经出现一次但是再次出现的
        twos = twos | (ones & A[i]);
        //出现一次的
        ones = ones | A[i];

        //当某一位出现三次后,我们就从出现两次中消除该位
        twos = twos & ~threes;
        //当某一位出现三次后,我们就从出现一次中消除该位
        ones = ones & ~threes;
    }
    //twos, threes最终都为0.ones是只出现一次的数
    return ones;
}
 
面试题40:数组中只出现一次的数字
3.一个整型数组中除了两个数字之外,其他数字都出现了两次,找出这两个只出现一次的数字。
 
根据问题1,从头到尾以后得到的是这两个出现一次的数字的异或的结果。这个异或的结果肯定不为0。
不为0,也就是说结果数字的二进制表示中至少就有一位为1。
我们从右到左找到这个第一个不为0的位置,然后利用这一位是不是0把这个数组分成两个子数组,
这两个子数组,中各包含一个只出现一次的数字,
为什么呢,因为,如果a^b的结果,某一位为1说明,a和b的这个位置是不同的!!!
而且如果这一位如果相同,那么它们可能是重复的数字,我们就把他们分配到同一个数组中!!!
 
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
#include <iostream>
using namespace std;

bool IsBit1(int num, unsigned int indexBit)
{
    num = num >> indexBit;
    return (num & 1);
}

unsigned int FindFirstBitIs1(int num)
{
    int indexBit = 0;
    while ( ((num & 1) == 0) && (indexBit < 8 * sizeof(int)) )
    {
        num = num >> 1;
        ++ indexBit;
    }
    return indexBit;
}

void FindNumsAppearOnce(int data[], int length, int *num1, int *num2)
{
    if (data == NULL || length < 2)
    {
        return ;
    }
    int resultExclusiveOR = 0;
    for (int i = 0; i < length; ++i)
    {
        resultExclusiveOR ^= data[i];
    }

    unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR);

    *num1 = *num2 = 0;
    for (int j = 0; j < length; ++j)
    {
        if (IsBit1(data[j], indexOf1))
        {
            *num1 ^= data[j];
        }
        else
        {
            *num2 ^= data[j];
        }
    }
}

int main()
{
    int data[] = {24363255};
    int length = sizeof(data) / sizeof(data[0]);
    int num1, num2;
    FindNumsAppearOnce(data, length, &num1, &num2);
    cout << num1 << " " << num2 << endl;
    return 0;
}
 
 
 
 
 
 
 
 
 

【01】数组中只出现一次的数字,布布扣,bubuko.com

【01】数组中只出现一次的数字

标签:c   style   code   a   ext   color   

原文地址:http://www.cnblogs.com/codemylife/p/3777245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!