Linear Time Voting Algorithm. I don't get it

前端 未结 5 692
南方客
南方客 2020-12-07 20:31

As I was reading this (Find the most common entry in an array), the Boyer and Moore\'s Linear Time Voting Algorithm was suggested.

If you follow the link to the si

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 21:12

    I wrote a C++ code for this algorithm

    char find_more_than_half_shown_number(char* arr, int len){
    int i=0;
    std::vector vec;
    while(i=(len+1)/2)
        return vec[0];
    else
        return -1;
    }
    

    and the main function is as below:

    int main(int argc, const char * argv[])
    {
        char arr[]={'A','A','A','C','C','B','B','C','C','C','B','C','C'};
        int len=sizeof(arr)/sizeof(char);
        char rest_num=find_more_than_half_shown_number(arr,len);
        std::cout << "rest_num="<

提交回复
热议问题