forked from zhedahht/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber.cpp
More file actions
120 lines (102 loc) · 2.77 KB
/
MissingNumber.cpp
File metadata and controls
120 lines (102 loc) · 2.77 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
/*******************************************************************
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——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题53(二):0到n-1中缺失的数字
// 题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字
// 都在范围0到n-1之内。在范围0到n-1的n个数字中有且只有一个数字不在该数组
// 中,请找出这个数字。
#include <cstdio>
int GetMissingNumber(const int* numbers, int length)
{
if(numbers == nullptr || length <= 0)
return -1;
int left = 0;
int right = length - 1;
while(left <= right)
{
int middle = (right + left) >> 1;
if(numbers[middle] != middle)
{
if(middle == 0 || numbers[middle - 1] == middle - 1)
return middle;
right = middle - 1;
}
else
left = middle + 1;
}
if(left == length)
return length;
// 无效的输入,比如数组不是按要求排序的,
// 或者有数字不在0到n-1范围之内
return -1;
}
// ====================测试代码====================
void Test(const char* testName, int numbers[], int length, int expected)
{
if(testName != nullptr)
printf("%s begins: ", testName);
int result = GetMissingNumber(numbers, length);
if(result == expected)
printf("Passed.\n");
else
printf("Failed.\n");
}
// 缺失的是第一个数字0
void Test1()
{
int numbers[] = { 1, 2, 3, 4, 5 };
int expected = 0;
Test("Test1", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 缺失的是最后一个数字
void Test2()
{
int numbers[] = { 0, 1, 2, 3, 4 };
int expected = 5;
Test("Test2", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 缺失的是中间某个数字0
void Test3()
{
int numbers[] = { 0, 1, 2, 4, 5 };
int expected = 3;
Test("Test3", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 数组中只有一个数字,缺失的是第一个数字0
void Test4()
{
int numbers[] = { 1 };
int expected = 0;
Test("Test4", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 数组中只有一个数字,缺失的是最后一个数字1
void Test5()
{
int numbers[] = { 0 };
int expected = 1;
Test("Test5", numbers, sizeof(numbers) / sizeof(int), expected);
}
// 空数组
void Test6()
{
int expected = -1;
Test("Test6", nullptr, 0, expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}