More than half of the number of times the output array appears, no output is 0

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
       int count;
       int length=numbers.size();
        for(int i=0;i<length;i++)
        {
            count=0;
            for(int j=0;j<length;j++)
            {
                if(numbers[i]==numbers[j])
                {
                    count++;
                }
            }
            if(count>length/2)
            {
                return numbers[i];
                break;
            }
            else
            count=0;

        }
        if(count==0)
        return count;
    }
};

Normal operation:

 #include <the iostream>
the using namespace STD;
int main ()
{
    // count exceeds half of the array number appears
    int A [] = {1,2,3,2,2,2,5,4,2};
    / / a int [] = {1,2,3,2,4,2,5,2,3};
    int length = the sizeof (a) / the sizeof (a [0]); // get the length of the array of static 
    int COUNT;
    for (int I = 0; I <length; I ++)
    {
        COUNT = 0; // must be cleared each cycle, do not forget !!!!!!! 
        for (int J = 0; J <length; ++ J)
        {
            IF (A [I] == A [J])
            {
                COUNT ++;
            }
        }
        IF (COUNT> length / 2)
        {
            COUT << A [I] << endl;
            BREAK;
        }
        else // If this number does not exist, the assignment 0 
        COUNT = 0;
    
    }
    IF (COUNT == 0)
    COUT COUNT << << endl;
    return 0;
}

Published 15 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/aaassslll147/article/details/104086829