【本文链接】

http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html

题目】

一个数组中有三个数字a、b、c只出现一次,其他数字都出现了两次。请找出三个只出现一次的数字。

分析

这是一道很新颖的关于位运算的面试题。在之前的博文34.数组中2个只出现一次的数字[Find two numbers which appear once]中分析了N=1和N=2的情况。

(1).N=1时,数组所有数字异或的结果即为a。

(2).N=2时,数组所有数字异或的结果等于a^b,根据a^b的二进制中最后一个1出现的位置,将数组分为2组;对每一组数字进行异或,即可求得a和b。

(3).N=3时,数组所有数字异或的结果等于a^b^c。此时该如何区分呢?如果我们能够找出其中一个只出现一次的数字,剩下两个只出现一次的数字就可以转换为N=2的情况。

具体思路如下:

(1). f(x) = x & (-x)所得的结果即是x最后一位1所在的位置。

(2). x = a ^ b ^ c。

(3). flag = f(x^a)^f(x^b)^f(x^c) 结果必有一位是1,因为f(m)^f(n)结果为0或者为2个1。

(4). f(x^a)^f(x^b)^f(x^c)的第m位为1,则x^a, x^b, x^c必有1个或者3个第m位为1。

(5). 用反证法可得,x^a, x^b, x^c只有一个第m位为1。

举个例子data={1,2,3,4,4,5,5,6,6}

x = a ^ b ^ c =1^2^3 = 000

x^a=001, x^b=010, x^c=011

f(x^a)=001, f(x^b)=010, f(x^c)=001

flag = f(x^a)^f(x^b)^f(x^c)=010,flag = f(flag)=010

f(x^b)==flag

first=b=1

second=3,third=1

完整代码如下:

代码】

 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 
// 58_FindNumbersAppearOnce.cpp : Defines the entry point for the console application.
//
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/27
*/

#include "stdafx.h"

// find number which appear once
void Find1NumberAppearOnce(int data[], int length, int &num)
{
    )
        return;

// get the exclusive or result of array
    // a
;
    ; i < length; ++i)
        xor ^= data[i];
    num = xor;
}

// get last 1 bit of n
// n=00001110--->00000010
unsigned int GetLast1Bit(int n)
{
    return n & (-n);
}

// find 2 numbers which appear once
void Find2NumbersAppearOnce(int data[], int length, int &num1, int &num2)
{
    )
        return;

// get the exclusive or result of array
    // a^b
;
    ; i < length; ++i)
        xor ^= data[i];

// find the last bit 1 of xor
    int flag = GetLast1Bit(xor);
    num1 = num2 = ;
    ; j < length; ++j)
    {
        // divide numbers in data into 2 groups by flag:
        // numbers in group1: the & result is 1
        // numbers in group2: the & result is 0
        if (flag & data[j])
        {
            num1 ^= data[j];
        }
        else
        {
            num2 ^= data[j];
        }
    }
}

// swap a and b
void myswap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

// find 3 numbers which appear once
/*
(1). f(x) = x & (-x)
(2). x = a ^ b ^ c
(3). flag = f(x^a)^f(x^b)^f(x^c)
(4). flag = f(flag)
(5). x^a, x^b, x^c, only one of three is 1 at m-bit
  f(x^a)==flag

for example:
  data={1,2,3,4,4,5,5,6,6}
  x = a ^ b ^ c =1^2^3 = 000
  x^a=001, x^b=010, x^c=011
  f(x^a)=001, f(x^b)=010, f(x^c)=001
  flag = f(x^a)^f(x^b)^f(x^c)=010
  flag = f(flag)=010
  f(x^b)==flag
  first=b=1
  second=3,third=1
*/
void Find3NumbersAppearOnce(int data[], int length, int &num1, int &num2, int &num3)
{
    )
        return;

// get the exclusive or result of array
    // a^b^c
;
    ; i < length; i++)
        xor ^= data[i];

;
    ; i < length; i++)
        flag ^= GetLast1Bit(xor ^ data[i]);
    flag = GetLast1Bit(flag);

// get the first unique number
;
    ; i < length; i++)
        if(GetLast1Bit(data[i] ^ xor) == flag)
            first ^= data[i];
    num1 = first;

// move the first number to the end of array
; i < length; i++)
    {
        if (first == data[i])
        {
            myswap(data[i], data[length - ]);
            break;
        }
    }

// get the second and third unique number
, num2, num3);
}

//=================================================================
// test cases
void test_base1(int data[], int length)
{
    int num1;
    Find1NumberAppearOnce(data, length, num1);
    printf("%d\n", num1);
}

void test_base2(int data[], int length)
{
    int num1, num2;
    Find2NumbersAppearOnce(data, length, num1, num2);
    printf("%d %d\n", num1, num2);
}

void test_base3(int data[], int length)
{
    int num1, num2, num3;
    Find3NumbersAppearOnce(data, length, num1, num2, num3);
    printf("%d %d %d\n", num1, num2, num3);
}

void test_case1()
{
    };
    int length = sizeof(data) / sizeof(int);
    test_base1(data, length);
}

void test_case2()
{
    };
    int length = sizeof(data) / sizeof(int);
    test_base2(data, length);
}

void test_case3()
{
    };
    int length = sizeof(data) / sizeof(int);
    test_base3(data, length);
}

void test_main()
{
    test_case1(); // 1
    test_case2(); // 1 2
    test_case3(); // 2 3 1
}
//=================================================================

int _tmain(int argc, _TCHAR *argv[])
{
    test_main();
    ;
}

【参考】

http://www.cnblogs.com/hellogiser/p/3738909.html

http://zhedahht.blog.163.com/blog/static/25411174201283084246412/

http://www.cnblogs.com/youxin/p/3349834.html

http://www.cnblogs.com/kedebug/archive/2012/12/22/2829013.html

【本文链接】

http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html

59. 总结篇:数组中N(n=1,2,3)个只出现一次的数字[find N numbers which appear only once in array]的更多相关文章

  1. 34.数组中2个只出现一次的数字[Find two numbers which appear once]

    [题目] 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). [分析] 这是一道很新颖的关于位运算的面试题. ...

  2. 转载——JavaScript学习笔记:取数组中最大值和最小值

    转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...

  3. JavaScript学习:取数组中最大值和最小值

    在实际业务中有的时候要取出数组中的最大值或最小值.但在数组中并没有提供arr.max()和arr.min()这样的方法.那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最 ...

  4. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  5. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...

  6. 剑指Offer 40. 数组中只出现一次的数字 (数组)

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 题目地址 https://www.nowcoder.com/practice/e02fdb54 ...

  7. 剑指Offer - 九度1349 - 数字在排序数组中出现的次数

    剑指Offer - 九度1349 - 数字在排序数组中出现的次数2013-11-23 00:47 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n, ...

  8. 剑指offer笔记面试题4----二维数组中的查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 测试用例: 二维数组中包含 ...

  9. 剑指offer第二版面试题1:数组中重复的数字(JAVA版)

    题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复的次数.请找出数组中任意一个重复的数字.例如如果输入长度为7的数组{ ...

随机推荐

  1. maven中Rhino classes (js.jar) not found - Javascript disabled的处理

    想使用单元测试 来测一下服务请求,于是想到了使用Junit,查了一下,决定使用 HttpUnit 来发送请求 于是在maven中引入了 <dependency> <groupId&g ...

  2. jQuery插件开发详细教程

    这篇文章主要介绍了jQuery插件开发详细教程,将概述jQuery插件开发的基本知识,最佳做法和常见的陷阱,需要的朋友可以参考下 扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间. ...

  3. BZOJ-2756 奇怪的游戏 黑白染色+最大流+当前弧优化+二分判断+分类讨论

    这个题的数据,太卡了,TLE了两晚上,各种调试优化,各种蛋疼. 2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MB Submit ...

  4. 微信支付(APP)集成时碰到的问题(.net提示“无权限”、iOS跳转到微信支付页面中间只有一个“确定”按钮)

    直入主题之前,请容我吐槽一下微*的官方东西:ASDFQ%#$%$#$%^FG@#$%DSFQ#$%.......:吐槽玩了!大家心照就好. 要完成手机APP跳转到微信的APP进行微信支付,需要进行如下 ...

  5. JAVA多线程经典范列:生产者与消费者

    * 第一:生产者 生产的消费品 存放到仓库中,当仓库满时,生产者停止生产 * 第二:消费者 到仓库中 使用消费品,当仓库没有消费品时,停止消费 * 第三:生产者 在仓库满停止生产后 通知消费者去消费 ...

  6. eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error

    在导入网友提供的Tomcat源码工程时候出现了 The project cannot be build until build path errors are resolved Unbound cla ...

  7. mysql 时间函数

    select UNIX_TIMESTAMP(Now());#获取unix时间戳1436430994 ::"时间形式

  8. 安装coreseek找不到mysql

    1.安装 coreseek-3.2.14 遇到问题:“ERROR: cannot find MySQL include files,随即在网上搜索各种答案说是要找到mysql.h的正确路径加入./co ...

  9. Extreme Learning Machine(ELM)的工程哲学

    Extreme Learning Machine(ELM)的工程哲学 David_Wang2015 发布于2015年5月6日 11:29 工程问题往往需要的是一定精度范围内的结果,而不是“真正的”结果 ...

  10. DIV+CSS例子

    DIV水平居中+垂直居中 #main_zone{ width:1190px; height:570px; background-color:#fff; margin:0 auto; /*左右居中*/ ...