The number of digital (c ++), more than half of the array appear to prove safety offer--

Title Description
array The number of times a number appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.

Thinking a
iterate two values are stored: a is a number in the figures, the number of times the other. When traversing to the next digit of the time, and if the next number is equal to the previously stored numbers, plus the number 1; if different, then decremented by 1; if the number is zero, then we need to save the next number, and set the number of times 1.
Due to the number of times we are looking for the number that appears even more than all the other times of the number that appears and then looking for numbers certainly be the last time the number corresponding to a number 1.

class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> nums) {
if(nums.size() <= 0){
return 0;
}
int res = nums[0];
int times = 1;
for(int i = 1; i < nums.size(); ++i){
if(times == 0){
res = nums[i];
times = 1;
}
else if(nums[i] == res){
times++;
}
else{
times--;
}
}
if(!CheckMoreHalf(nums, res)){
res = 0;
}
return res;
}
private:
//判断这个数字是否出现次数超过一半
bool CheckMoreHalf(vector<int>& nums, int result){
int times = 0;
for(int i = 0; i < nums.size(); ++i){
if(nums[i] == result){
++times;
}
}
if(times * 2 <= nums.size()){
return false;
}
return true;
}
};
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
思路二
Fast row based on the idea. After the sorted array, the middle number is the number to look for, then this data is a statistical median, i.e., an array of length n, the first n / 2 large number.
Fast random row, a randomly selected number, the adjustment order of the array, if it happens to be the subscript n / 2, then the median found; if it is less than index n / 2, then it should be the median of the right; if it is larger than the subscript n / 2, then the median should its left.

class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> nums) {
int n = nums.size();
if(n <= 0){
return 0;
}
int left = 0, right = n - 1;
int mid = n / 2;
int index = Partition(nums, left, right);
while(index != mid){
if(index > mid){
right = index - 1;
index = Partition(nums, left, right);
}
else{
left = index + 1;
index = Partition(nums, left, right);
}
}
int res = nums[mid];
if(!CheckMoreHalf(nums, res)){
res = 0;
}
return res;
}
private:
int Partition(vector<int>& nums, int left, int right){
int pivot = nums[right];
int tail = left - 1;
for(int i = left; i < right; ++i){
if(nums[left] <= pivot){
++tail;
if(tail != i)
swap(nums[tail], nums[i]);
}
}
swap(nums[tail+1], nums[right]);
return tail+1;
}
bool CheckMoreHalf(vector<int>& nums, int result){
int times = 0;
for(int i = 0; i < nums.size(); ++i){
if(nums[i] == result){
++times;
}
}
if(times * 2 <= nums.size()){
return false;
}
return true;
}
};
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

---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/11109196.html