Offer 28: Find more than half the number in the array.

Posted by geroido on Mon, 26 Aug 2019 18:50:52 +0200

1 Title Description

There is a number in the array that occurs more than half the length of the array. Find the number.For example, enter an array {1,2,3,2,2,2,5,4,2} with a length of 9.Since the number 2 appears five times in the array, more than half the length of the array, output 2.If not, output 0.

2 Ideas and methods

(1) Hash table

Hash tables are used to record the number of occurrences of each element, returning 1, time complexity O(n), space complexity O(n) if the occurrence of the element is more than half.m[numbers[i]]+=1; map<int, int>m;

(2) Sorting

Sort first, take the middle number, if this number is more than half the length in the array, it exists; otherwise, there is no time complexity O (nlogn) sort; spatial complexity O (1).

(3) Find the median O(n)

Based on the partiton function O(n), if present: the number occurs more than half the time, the second/n element in the sort is that element; that is, the median

3 C++ Core Code

(1) Hash table m[numbers[i]+=1; map<int, int>m;

 1 class Solution {
 2 public:
 3     int MoreThanHalfNum_Solution(vector<int> numbers) {
 4         // Hash table stores the number of times a number occurs hash Query time complexity O(1);Total Time Complexity O(n)
 5         map<int, int> m;
 6         for (int i = 0; i < numbers.size(); ++i) {
 7             m[numbers[i]]+=1;
 8             if(m[numbers[i]]>numbers.size()/2)
 9                 return numbers[i];
10         }
11         return 0;
12     }
13 };

(2) Sorting

 1 class Solution {
 2 public:
 3     int MoreThanHalfNum_Solution(vector<int> numbers) {
 4         sort(numbers.begin(),numbers.end());
 5         int key = numbers[numbers.size()/2];
 6         int count = 0;
 7         for (int i = 0; i < numbers.size(); ++i) {
 8             if(key == numbers[i])
 9                 ++ count;
10         }
11         if (count>numbers.size()/2)
12             return key;
13         return 0;
14     }
15 };

(3) Find Median O(n) - Complete Code

 1 #include <iostream>
 2 int Partition(int A[], int low, int high)
 3 {
 4     int pivot = A[low];
 5     while (low <high)
 6     {
 7         while (low<high && A[high] >= pivot) --high;
 8         A[low] = A[high];
 9         while (low<high && A[low] <= pivot) ++low;
10         A[high] = A[low];
11     }
12     A[low] = pivot;
13     return low;
14 }
15 int HalfData(int a[], int len)
16 {
17     int start = 0;
18     int end = len - 1;
19     int middle = len >> 1;
20     int index = Partition(a, start, end);
21 
22     while (index != middle)
23     {
24         if (index > middle)
25         {
26             end = index - 1;
27             index = Partition(a, start, end);
28         }
29         else
30         {
31             start = index + 1;
32             index = Partition(a, start, end);
33         }
34     }
35     return a[index];
36 }
37 int main()
38 {
39     int a[9] = { 1, 2, 3, 2, 2, 2, 5, 4, 2 };
40     int len = 9;
41     int result = HalfData(a, 9);
42     printf("result:%d\n", result);
43 
44     system("pause");
45     return 0;
46 }

4 C++ Complete Code

 1 #include <iostream>
 2 #include <vector>
 3  
 4 using namespace std;
 5  
 6 int MoreThanHalfNum(vector<int> numbers) 
 7 {
 8     if (numbers.size() == 0)
 9     {
10         return 0;
11     }
12  
13     int target = numbers[0];
14     unsigned int cnt = 1;
15     
16     for (unsigned int i = 1; i < numbers.size(); ++i)
17     {
18         if (target == numbers[i])
19         {
20             cnt++;
21         }
22         else
23         {
24             cnt--;
25         }
26  
27         if (cnt == 0)
28         {
29             cnt = 1;
30             target = numbers[i];
31         }
32     }
33     cnt = 0;
34     for (unsigned int i = 0; i < numbers.size(); ++i)
35     {
36         if (target == numbers[i])
37         {
38             cnt++;
39         }
40     }
41  
42     if (cnt * 2 > numbers.size())
43     {
44         return target;
45     }
46  
47     return 0;
48 }
49  
50 int main(void)
51 {
52     int a[] = {1,2,2,2,3,4,2,5,2};
53     vector<int> v(a, a + 9);
54  
55     cout<<MoreThanHalfNum(v)<<endl;
56     
57     return 0;
58 }

https://blog.csdn.net/u013575812/article/details/50130307

Time Complexity O(n).The first number of arrays is initially thought to be the target number.Then iterate through the elements behind the array, counting ++, if equal to the number of targets; otherwise counting--; if you find the number of targets <=0, the current number of targets is not the final output.The number of update targets is the element that is currently traversed.Verify the result after traversing all the elements of the array.

Reference material

https://blog.csdn.net/zjwreal/article/details/88607992

https://blog.csdn.net/u013575812/article/details/50130307

Topics: C++