[LeetCode] Majority Element II

Source: Internet
Author: User

[LeetCode] Majority Element II

Given an integer array of size n, find all elements that appear more⌊ n/3 ⌋Times. The algorithm shocould run in linear time and in O (1) space.

Solutions

Moore's voting method. The core of the voting method is to find two candidate modes for voting. It needs to be traversed twice. The first traversal is to find two candidate modes, and the second traversal is to re-vote to verify whether the two candidate modes are public.

Implementation Code

C ++:

class Solution {public:    vector
  
    majorityElement(vector
   
    & nums) {        vector
    
      res;        int m = 0, n = 0, cm = 0, cn = 0;        for (int i = 0; i < nums.size(); i++)        {            if (nums[i] == m)            {                cm++;            }            else if (nums[i] == n)            {                cn++;            }            else if (cm == 0)            {                m = nums[i];                cm = 1;            }            else if (cn == 0)            {                n = nums[i];                cn = 1;            }            else            {                --cm;                --cn;            }        }        cm = cn = 0;        for (auto& a : nums)        {            if (a == m)            {                cm++;            }            else if (a== n)            {                cn++;            }        }        if (cm > nums.size() / 3)        {            res.push_back(m);        }        if (cn > nums.size() / 3)        {            res.push_back(n);        }        return res;    }};
    
   
  

Java:

public class Solution {    public List
  
    majorityElement(int[] nums) {        List
   
     res = new ArrayList
    
     ();        int m = 0, n = 0, cm = 0, cn = 0;        for (int a : nums) {            if (a == m) {                ++cm;            } else if (a == n) {                ++cn;            } else if (cm == 0) {                m = a;                cm = 1;            } else if (cn == 0) {                n = a;                cn = 1;            } else {                --cm;                --cn;            }        }        cm = cn = 0;        for (int a : nums){            if (a == m) {                ++cm;            } else if (a == n) {                ++cn;            }        }        if (cm > nums.length / 3) {            res.add(m);        }        if (cn > nums.length / 3) {            res.add(n);        }        return res;    }}
    
   
  

 

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.