leetcode - majority elements

原创 2015年07月10日 17:01:40

leetcode - majority elements

给定size 为n的数组,查找出主元素,就是出现次数大于n/2次的元素。

1.

 int majorityElement(vector<int> &num) { 
std::map<int, int> im; 
for (int i = 0; i < num.size(); ++i){ 
map<int, int>::iterator it = im.find(num[i]);
 if (it == im.end()) 
{ im[num[i]] = 1; } 
else 
{ im[num[i]]++; } 
if (im[num[i]] > num.size()/2) 
{ return num[i]; } }
 return 0;}
2.
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int ret = nums[0];
        int count = 1;
        for(int i = 1; i < nums.size(); i ++)
        {
            if(nums[i] == ret)
                count ++;
            else
            {
                count --;
                if(count == 0)
                {
                    ret = nums[i];
                    count = 1;
                }
            }
        }
        return ret;
    }
};
3. Moore's voting algorithm
int majorityElement(vector<int> &num) {    
int majorityIndex = 0;    
for (int count = 1, i = 1; i < num.size(); i++) 
{        
num[majorityIndex] == num[i] ? count++ : count--;        
if (count == 0) {           
 majorityIndex = i;            
count = 1;      
  }    
}    
return num[majorityIndex];
}
4.
class Solution {
 2 public:
 3     int majorityElement(vector<int> &num) {
 4 
 5         int count = 0;
 6         
 7         for(;;) {
 8             if(num.size() == 1)
 9                 return num[0];
10             else    {
11                 int i = rand() % (num.size() - 1);
12                 for(int j = 0; j < num.size(); j++) {
13                     if(num[j] == num[i])
14                         count++;
15                 }
16                 if(count > (num.size() / 2))
17                     return num[i];
18                 else    {
19                     count = 0;
20                     continue;
21                 }
22             }
23         }
24     }
5.
 int my_cmp(int a,int b)  
{      
return a > b; 
 }    
int Solution::majorityElement(vector<int> &num) 
 {      
sort(num.begin(),num.end(),my_cmp);     
 return num[num.size()/2];
  }  

相关文章推荐

LeetCode Majority Element

链接: https://oj.leetcode.com/problems/majority-element/ LeetCode给的这道题的解答真的不错: Runtime: O(n2) — ...

《leetCode》:Majority Element II

题目Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo...

【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)

Question1Given an array of size n, find the majority element. The majority element is the element th...

[leetcode] 169.Majority Element

题目: Given an array of size n, find the majority element. The majority element is the element that a...

LeetCode || Majority Element

方法一: 按照顺序循环,如果两个元素不相等,则删除这两个元素。则剩下的为出现大于n/2次的那个元素。 耗时:144ms。 class Solution { public: int majo...
  • xsloop
  • xsloop
  • 2015年07月22日 10:10
  • 339

leetcode[169]:Majority Element

Majority Element Given an array of size n, find the majority element. The majority element is the e...

LeetCode[Array]: Majority Element

Given an array of size n, find the majority element. The majority element is the element that appear...
  • chfe007
  • chfe007
  • 2015年01月20日 16:15
  • 783

leetcode 229:Majority Element II

根据摩尔投票解决majority问题

【LEETCODE】169-Majority Element

Given an array of size n, find the majority element. The majority element is the element that appear...

LEETCODE-Majority Element

Majority Element Given an array of size n, find the majority element. The majority element is the e...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode - majority elements
举报原因:
原因补充:

(最多只允许输入30个字)