-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoreThanHalfNum.cpp
More file actions
82 lines (73 loc) · 2.1 KB
/
MoreThanHalfNum.cpp
File metadata and controls
82 lines (73 loc) · 2.1 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
#include "array_util.h"
#include <exception>
/// 39 数组中出现次数超过一半的数字
/// help check functions
bool g_bInputInvalid = false;
/// 个人觉得不是很有必要 最好使用vector<>来替换,可以减少这种无效输出的检查
bool CheckInvalidArray(int* numbers, int length)
{
g_bInputInvalid = false;
if (numbers == nullptr && length <= 0)
g_bInputInvalid = true;
return g_bInputInvalid;
}
bool CheckMoreThanHalf(int* numbers, int length, int number)
{
int times = 0;
for (int i = 0; i < length; ++i) {
if (numbers[i] == number)
times++;
}
bool isMoreThanHalf = true;
if (times * 2 <= length) {
g_bInputInvalid = true;
isMoreThanHalf = false;
}
return isMoreThanHalf;
}
int MoreThanHalfNum_v1(int* numbers, int length)
{
if (CheckInvalidArray(numbers, length))
return 0;
int middle = length / 2;
int start = 0;
int end = length - 1;
int index = Partition(numbers, length, start, end);
while (index != middle) {
if (index > middle) {
end = index - 1;
index = Partition(numbers, length, start, end);
} else {
start = index + 1;
index = Partition(numbers, length, start, end);
}
}
int result = numbers[middle];
if (!CheckMoreThanHalf(numbers, length, result))
result = 0;
return result;
}
/// v2 超过一半的数字保证那个数字可以抵消所有其他的数字
int MoreThanHalfNum_v2(int* numbers, int length)
{
if (CheckInvalidArray(numbers, length))
return 0;
int result = numbers[0]; //当前的数字
int times = 1; //当前数字出现的次数
for (size_t i = 1; i < length; i++) {
/// 次数降为0时 换新的数字
if (times == 0) {
result = numbers[i];
times = 1;
} else {
if (numbers[i] == result) {
++times;
} else {
--times;
}
}
}
if (!CheckMoreThanHalf(numbers, length, result))
return 0;
return result;
}