The number of digital (C ++ wins the Offer Detailed) More than half of the array appear

The number of digital (C ++ wins the Offer Detailed) More than half of the array appear
The first time I saw this question, simply to feel the explosion, not that sort function row a sequence, taking the median, count the number of traverse again not to OK it? But during the interview, sort might make to achieve your own, or that the questions asked: Can not modify array input it? ? ?
The first method (based on Partition Function)
Note: This method modifies the input array

Core: More than half of the number of occurrences of numbers, after ordering the median figure is certainly

Problem-solving ideas: Through Partition function, a randomly selected element in the array (in my code default is the first element of the array), and ranked by the idea of ​​fast, small elements moved to the front of a random value, large elements random values ​​moved to the back, and then returns the final position of the random value. Partition by calling the function, with the index value and the value for returning the judgment, if index> mid, left median description, end = mid-1, if index <mid, description places to the right, start = mid +1, until finally equals mid, and numbers [mid] as the most number appears, and finally through the Check function, determines whether the number exceeds half appears array

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers)
    {
        int length = numbers.size();
        if (length <= 0)//判空
            return 0;
        int start = 0;
        int end = length - 1;
        int index = Partition(numbers, start, end);//Partition函数随机值在数组中的位置
        int mid = length >> 1;//统计学的中位数
        while (index != mid)//随机值与中位数位置的判等
        {
            if (index>mid)
            {
                end = index - 1;
                index = Partition(numbers, start, end);
            }
            else if (index<mid)
            {
                start = index + 1;
                index = Partition(numbers, start, end);
            }
        }
        if (!Check(numbers, numbers[index]))//判断次数是否超过一半
            return 0;
        return numbers[index];
    }
    int Partition(vector<int> num, int start, int end)
    {
        int index = start;
        int sum = num[start];//默认随机值为首位,这里可以用“随机函数”
        //从第二位开始
        for (int i = start + 1; i<end; ++i)
        {
            if (num[i]<sum)
            {
                ++index;
                swap(num[i], num[index]);
            }
        }
        swap(num[start], num[index]);//随机值居中(左小右大)
        return index;
    }
    bool Check(vector<int> num, int result)
    {
        int count = 0;
        for (int i = 0; i<num.size(); ++i)
        {
            if (num[i] == result)
                ++count;
        }
        if (count * 2>num.size())
            return true;
        return false;
    }
};

The second method (counting method)
NOTE: This method does not modify the input array

Core: The advent of digital More than half of the number of times than all other digits at least one more

Problem-solving ideas: the first from the beginning (result == numbers [0]), encountered equal elements, count ++, they are not equal elements, count--, when the count == 0, result reassigned to the current element, re-assigned to count 1, until after the completion of an array traversal, returns the result

doubt? ? ? Now, judge them, why call the Check function, because we can not determine whether the result is the highest number of elements appear, even the largest number of elements appear, I do not know whether more than half of the array, for example: {5,5,5,5 , 3,3,3,4,4,4,4,5,5,5,5,5} {5,3,3,3,4,4,4,4}

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        if(numbers.size()<=0)
            return 0;
        int result=numbers[0];
        int i=1;
        int count=1;
        for(;i<numbers.size();++i)
        {
            if(count==0)
            {
                result=numbers[i];
                count=1;
            }
            else if(result==numbers[i])
                ++count;
            else
                --count;
        }
        if(!Check(numbers,result))
                result=0;
        return result;
    }
    bool Check(vector<int> num,int result)
    {
        int count=0;
        for(int i=0;i<num.size();++i)
        {
            if(num[i]==result)
                ++count;
        }
        if(count*2>num.size())
            return true;
        return false;
    }

Guess you like

Origin blog.51cto.com/14233078/2469750