169[Esay]:Majority Element

本文探讨了在数组中查找出现次数超过一半的多数元素的有效算法。介绍了三种解决方案:双重循环计数法、桶排序改进法及摩尔投票算法。最终确定摩尔投票算法为最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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

You may assume that the array is non-empty and the majority element always exist in the array.

解题思路:

solution1:统计出数组中每个元素出现的次数,出现次数最多的即为所求。一开始使用2重循环,不管元素重不重复都统计次数,去leetcode上检测“Time Limit Exceeded”。


solution2:用桶排序来统计元素次数,下面贴的代码,在本地编译器上测试没问题,去leetcode上检测“Runtime Error”。

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cmath>  
  3. #include<vector>  
  4. using namespace std;  
  5.   
  6. int majorityElement(vector<int>& nums) {  
  7.     int length = nums.size();    
  8.       
  9.     // calculate the count the majorityElement should appear  
  10.     int number = 0;  
  11.     if (length % 2) {  
  12.         number = (length - 1) / 2;  
  13.     } else {  
  14.         number = length / 2;  
  15.     }  
  16.       
  17.     int max = 0;  
  18.     for (int i = 0; i < length; i++) {  
  19.         // the element in the vector might be non-positive  
  20.         if (abs(nums[i]) > max) {  
  21.             max = abs(nums[i]);  
  22.         }  
  23.     }  
  24.       
  25.     int size1 = 2*max + 1;  
  26.     int count[size1];  
  27.     for (int i = 0; i < size1; i++) {  
  28.         count[i] = 0;  
  29.     }  
  30.       
  31.       
  32.       
  33.     for (int i = 0; i < length; i++) {  
  34.         if (nums[i] < 0) {  
  35.             count[size1 + nums[i]]++;  
  36.             if (count[size1 + nums[i]] > number) {  
  37.             return nums[i];  
  38.             }  
  39.         } else {  
  40.             count[nums[i]]++;  
  41.             if (count[nums[i]] > number) {  
  42.             return nums[i];  
  43.             }  
  44.         }  
  45.     }  
  46. // 把桶排的数组大小缩一半也没有用  
  47.     /* 
  48.     int size1 = max + 1; 
  49.     int count[size1]; 
  50.     for (int i = 0; i < size1; i++) { 
  51.         count[i] = 0; 
  52.     } 
  53.      
  54.     int count1 = 0, max1; 
  55.     int count2 = 0, max2; 
  56.     for (int i = 0; i < length; i++) { 
  57.         if (nums[i] > 0 || nums[i] == 0) { 
  58.             count[nums[i]]++; 
  59.             if (count[nums[i]] > count1) { 
  60.                 count1 = count[nums[i]]; 
  61.                 max1 = nums[i]; 
  62.                 if (count1 > number) { 
  63.                     return max1; 
  64.                 } 
  65.             } 
  66.         } 
  67.     } 
  68.     for (int i = 0; i < length; i++) { 
  69.         if (nums[i] < 0) { 
  70.             count[size1 + nums[i]]++; 
  71.             if (count[size1 + nums[i]] > count2) { 
  72.                 count2 = count[size1 + nums[i]]; 
  73.                 max2 = nums[i]; 
  74.                 if (count2 > number) { 
  75.                     return max2; 
  76.                 } 
  77.             } 
  78.         } 
  79.     } 
  80.     */  
  81. }  
  82.   
  83. int main() {  
  84.     int n, temp;  
  85.     cin >> n;  
  86.     vector<int> nums;  
  87.     for (int i = 0; i < n; i++) {  
  88.         cin  >> temp;  
  89.         nums.push_back(temp);  
  90.     }  
  91.     cout << majorityElement(nums);  
  92.     return 0;  
  93. }  

solution3:参考leetcode上提供的解题思路——初始化众数majority的值为0,出现的次数为0;遍历整个数组,当前的nums[i]如果等于当前的众数则count++,majority等于当前的nums[i],否则count--。当然本题中题目说明众数的数目会大于n/2,是上述办法可行的前提。(以下代码已通过leetcode测试)

int majorityElement(vector<int>& nums) {
	int length = nums.size();
	int count = 0;
	int majority = 0;
	for (int i = 0; i < length; i++) {
		if ((majority == nums[i]) || (count == 0)) {
			count ++;
			majority = nums[i];
		} else {
			count--;
		}
	}
	return majority;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值