-
Notifications
You must be signed in to change notification settings - Fork 44
/
more_than_half_number.cpp
144 lines (133 loc) · 3.39 KB
/
more_than_half_number.cpp
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
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <cstdlib>
using namespace std;
int random_in_range(int start, int end)
{
if(start==end) {
return start;
} else {
int index = rand()%(end-start) + start;
return index;
}
}
template <typename T>
void swap(T* v1, T* v2)
{
T temp = *v1;
*v1 = *v2;
*v2 = temp;
}
int partition(int data[], int length, int start, int end)
{
if(data==NULL || length<=0 || start<0 || end>=length) {
return false;
}
int index = random_in_range(start, end);
swap<int>(&data[index], &data[end]);
int small = start-1;
for(index=start; index<end; ++index) {
if(data[index]<data[end]) {
++small;
if(small!=index) {
swap<int>(&data[index], &data[small]);
}
}
}
++small;
swap<int>(&data[small], &data[end]);
return small;
}
bool inline check_input(int *numbers, int length)
{
if (!numbers || length <= 0)
return false;
return true;
}
bool check_more_than_half(int *numbers, int length, int number)
{
int count = 0;
for (int i=0; i<length; i++) {
if (numbers[i] == number)
count++;
}
if ((count * 2) > length)
return true;
return false;
}
//此方法会改变原数组的顺序
int find_more_than_half_number_partition(int *numbers, int length)
{
if (check_input(numbers, length)) {
int start = 0;
int end = length - 1;
int middle = length >> 1;
// start、end 为数组对应的 offset
int index = partition(numbers, length, start, end);
while (index != middle) {
if (index < middle) {
start = index + 1;
index = partition(numbers, length, start, end);
} else {
end = index - 1;
index = partition(numbers, length, start, end);
}
}
int res = numbers[middle];
if (check_more_than_half(numbers, length, res))
return res;
else
return -1;
}
}
//此方法仅读取数组,不会改变原数组的顺序
int find_more_than_halt_number_loop(int *numbers, int length)
{
if (check_input(numbers, length)) {
int res = numbers[0];
int count = 1;
for (int i=1; i<length; i++) {
if (count == 0) {
res = numbers[i];
count = 1;
} else if (res == numbers[i]) {
count++;
} else {
count--;
}
}
if (check_more_than_half(numbers, length, res))
return res;
else
return -1;
}
}
int main(int argc, char** argv)
{
int numbers_test1[7] = {6, 8, 5, 5, 5, 5, 3};
int numbers_test2[7] = {6, 8, 0, 5, 5, 0, 3};
int res = find_more_than_half_number_partition(numbers_test1, 7);
cout << res << endl;
res = find_more_than_half_number_partition(numbers_test2, 7);
cout << res << endl;
res = find_more_than_halt_number_loop(numbers_test1, 7);
cout << res << endl;
res = find_more_than_halt_number_loop(numbers_test2, 7);
cout << res << endl;
return 0;
}