Prove safety offer28: find an array of digital more than half.

1 Title Description

  There are a number of array number that 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.

2 ideas and methods

  (1) hash table

  Hash table with the number of records of each element appears, if the number of times the element occurs more than half, returns 1, the time complexity of O (n), the spatial complexity of O (n).m[numbers[i]]+=1map<int, int> m;

  (2) Sort

  To sort, middle access, if this number appears in more than half the length of the array, there; otherwise, the time complexity of O (nlogn) sorting absent; space complexity of O (1).

  (3) Find the median O (n)

  Based partiton function O (n), if present: more than half of the number of occurrences, sorting of 2 / n elements is the element; is the median

3 C ++ core code

(1) hash table m[numbers[i]]+=1map<intint> m;

. 1  class Solution {
 2  public :
 . 3      int MoreThanHalfNum_Solution (Vector < int > Numbers) {
 . 4          // number of times the hash table storing the number of occurrences of a hash query time complexity of O (1); the total time complexity of 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 };
View Code

(2) Sort

 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 };
View Code

(3) Find the median O (n) - the full 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 }
View Code

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 }
View Code

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

  Time complexity of O (n). The first number is the initial array think the target number (target). After traversing the back of the array elements, if the number is equal to the target count ++; else count -; if the count of the number of targets found <= 0 shows that the current target number is not the final output. Update the target number for the current traverse to the elements. Verify through the array after completing all the elements at the results can be.

Reference material

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

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

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11415849.html