forked from zhedahht/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberAppearingOnce.cpp
More file actions
146 lines (125 loc) · 3.46 KB
/
NumberAppearingOnce.cpp
File metadata and controls
146 lines (125 loc) · 3.46 KB
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
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题56(二):数组中唯一只出现一次的数字
// 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次。请
// 找出那个吃出现一次的数字。
#include <cstdio>
#include <exception>
int FindNumberAppearingOnce(int numbers[], int length)
{
if(numbers == nullptr || length <= 0)
throw new std::exception("Invalid input.");
int bitSum[32] = {0};
for(int i = 0; i < length; ++i)
{
int bitMask = 1;
for(int j = 31; j >= 0; --j)
{
int bit = numbers[i] & bitMask;
if(bit != 0)
bitSum[j] += 1;
bitMask = bitMask << 1;
}
}
int result = 0;
for(int i = 0; i < 32; ++i)
{
result = result << 1;
result += bitSum[i] % 3;
}
return result;
}
// ====================测试代码====================
void Test(const char* testName, int numbers[], int length, int expected)
{
int result = FindNumberAppearingOnce(numbers, length);
if(result == expected)
printf("%s passed.\n", testName);
else
printf("%s FAILED.\n", testName);
}
// 所有数字都是正数,唯一的数字是最小的
void Test1()
{
int numbers[] = { 1, 1, 2, 2, 2, 1, 3 };
int expected = 3;
Test("Test1", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 所有数字都是正数,唯一的数字的大小位于中间
void Test2()
{
int numbers[] = { 4, 3, 3, 2, 2, 2, 3 };
int expected = 4;
Test("Test2", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 所有数字都是正数,唯一的数字是最大的
void Test3()
{
int numbers[] = { 4, 4, 1, 1, 1, 7, 4 };
int expected = 7;
Test("Test3", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 唯一的数字是负数
void Test4()
{
int numbers[] = { -10, 214, 214, 214 };
int expected = -10;
Test("Test4", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 除了唯一的数字,其他数字都是负数
void Test5()
{
int numbers[] = { -209, 3467, -209, -209 };
int expected = 3467;
Test("Test5", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 重复的数字有正数也有负数
void Test6()
{
int numbers[] = { 1024, -1025, 1024, -1025, 1024, -1025, 1023 };
int expected = 1023;
Test("Test6", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 所有数字都是负数
void Test7()
{
int numbers[] = { -1024, -1024, -1024, -1023 };
int expected = -1023;
Test("Test7", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 唯一的数字是0
void Test8()
{
int numbers[] = { -23, 0, 214, -23, 214, -23, 214 };
int expected = 0;
Test("Test8", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 除了唯一的数字,其他数字都是0
void Test9()
{
int numbers[] = { 0, 3467, 0, 0, 0, 0, 0, 0 };
int expected = 3467;
Test("Test9", numbers, sizeof(numbers) / sizeof(int), expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
return 0;
}