[Leetcode] Majority Element

Source: Internet
Author: User

Majority Element

Given an array of size n, find the majority element. The majority element is the element, the appears more than times ? n/2 ? .

Assume that the array was non-empty and the majority element always exist in the array.


Problem Solving Ideas:

1. Use a map to record the number of occurrences of each number. If a count of a number is greater than N/2, the number is returned.

Class Solution {public:    int majorityelement (vector<int> &num) {        int len = Num.size ();        Map<int, int> count;        for (int i=0; i<len; i++) {            count[num[i]]++;            if (Count[num[i]] > LEN/2) {                return num[i];            }        }        return 0;}    };
2, for a sorted array, if the majority exists, the majority of the number must be the middle.
Class Solution {public:    int majorityelement (vector<int> &num) {        std::sort (Num.begin (), Num.end ());        return Num[num.size ()/2];    };
3, voting algorithm. Can think of as Daleitai, on stage that person if win and lose the same number of times, then step down, finally defeated his people on stage. The majority is certainly the last winner. This algorithm uses the least time.

Class Solution {public:    int majorityelement (vector<int> &num) {        int len = Num.size ();        if (len==0) {            return 0;        }        int candidate = num[0];        int count = 1;        for (int i=1; i<len; i++) {            if (num[i]==candidate) {                count++;            } else{                count--;            }            if (count==0) {                candidate = num[i];                count=1;            }        }        return candidate;}    ;
4, bit algorithm. Considering that each number can be represented by a 32-bit binary, the count of each digit binary is 1, if a bits count is greater than N/2, there must be a majority contribution. This approach is novel, although the speed is not comparable to voting algorithms, but pioneering thinking. To say here, the first method, defines Map<int, int> count, for each newly added key value, its value defaults to 0, but for the int array type, each array is initialized to a random value, so use the Memset function.

Class Solution {public:    int majorityelement (vector<int> &num) {        int len = Num.size ();        if (len==0) {            return 0;        }        int bitcount[32];        memset (bitcount, 0, 32*sizeof (int.));        for (int i=0, i<len; i++) {for            (int j=0; j<32; J + +) {                if (num[i]& (1<<j)) {  //J bit is 1                    bitcount[j]++        ;        }}} int result=0;        for (int i=0; i<32; i++) {            if (Bitcount[i] > LEN/2)         //I-bit is 1 count greater than half, there must be a contribution of the majority                result + = (int) Pow (2, i) ;        }        return result;}    ;



[Leetcode] Majority Element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.