Q39 The number of occurrences in the array exceeds half

More than half of the occurrences in the array

topic

There is a number in the array that appears more than half the length of the array. Please find this number. For example, enter an array of length {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, more than half the length of the array, so output 2. If it does not exist, output 0.

Ideas

Idea 1

Important premise: If a number repeats more than half the length of the array, the middle number of the array must be the repeated number.

Therefore, you can have the following ideas:

  1. Use the fast-sorting partition function to continuously find the pivot in the median of the array.
    1. If the index returned by partition is to the left of mid, continue searching at [index + 1, right]
    2. If the index returned by partition is to the right of mid, continue searching at [left, index-1]
    3. index == mid, return
  2. If the pivot is repeated more than half times in the array, output,
  3. Otherwise, the output is none.

Idea 2

There is another idea:

The same figure offsets the idea.

Maintain a current number t and the current number of times n.

  1. During initialization, t is arr [0], n = 1
  2. Continue to go down, if arr [i] is the same as t, then n + 1, if not, n-1
  3. If n == 0, then t becomes the number, n + 1

End:

  • If a certain number of times exceeds half, the last thing left is definitely it.
  • The figures given at the end must be judged. Iterate again to verify.

achieve

There are two errors when programming:

  1. left = pIndex + 1; and right = pIndex-1;, there was no increment and decrement of 1 at the time, which caused a continuous loop and the pIndex remained unchanged.
  2. if (times <= mid) does not add or equal, resulting in an array of size 9, when the repeated number is 4, no return does not exist. The best way to write it should be times * 2> length.
//思路1实现
class Solution {
public:
    int partition(vector<int>& numbers, int left, int right)
    {
        if(numbers.empty() || left<0 || right>=numbers.size() || left>right)
            return -1;
        int pivot = numbers[left];
        while(left<right)
        {
            while(left<right && numbers[right]>=pivot)
                --right;
            numbers[left] = numbers[right];
            while(left<right && numbers[left]<=pivot)
                ++left;
            numbers[right] = numbers[left];
        }
        numbers[left] = pivot;
        return left;
    }
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        int length = numbers.size();
        if(length==0) return 0;
        int left = 0;
        int right = length-1;
        int mid = length/2;
        int pIndex = partition(numbers, left, right);
        while(pIndex!=mid)
        {
            if(pIndex<mid)
            {
                left = pIndex+1;  //注意这里要加1
                pIndex = partition(numbers, left, right);
            }
            else
            {
                right = pIndex-1; //注意这里要改变
                pIndex = partition(numbers, left, right);
            }
        }
        int targetNum = numbers[pIndex];
        int times = 0;
        for(auto t : numbers)
        {
            if(t == targetNum)
                ++times;
        }
        if(times<=mid) //注意临界值判断,其实 if(!(2*times>length))更直观
            return 0;
        return targetNum;
    }
};
//思路2实现
class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        if(numbers.empty()) return 0;
        int target = numbers.front();
        int n = 1;
        for(int i=1; i<numbers.size(); ++i)
        {
            if(n==0)
            {
                target = numbers[i];
                ++n;
            }
            else
            {
                if(target == numbers[i])
                    ++n;
                else
                    --n;
            }
        }
        if(n<=0)
            return 0;
        int times = 0;
        for(auto val:numbers)
        {
            if(val == target)
                ++times;
        }
        if(2*times>numbers.size())
            return target;
        else
            return 0;            
    }
};
Published 58 original articles · won 11 · 30,000+ views

Guess you like

Origin blog.csdn.net/mhywoniu/article/details/105606266