leetcode-Majority Element II-229

输入一个数组,求出现次数超过n/3的元素
这是求出现次数超过n/k的元素系列
把数组以每组k个元素分成n/k,或者n/k+1组(当n%k!=0)时;既然要出现超过n/k次,假设这个元素存在,那么这个元素肯定至少在每组中出现一次,并且可能这样的元素有k-1个,因此还是用抵消法
以求超过n/2的元素为例,既然有个元素出现的次数超过一半,那么把它和其他的元素抵消,最后剩下的还是它
同理,求n/3,也是用非候选的元素和候选元素抵消,最后剩下两个候选元素还要遍历一遍数组求它们出现的具体次数来判断是否真的出现n/3次,因为这里我们假设有2个元素都出现超过n/3次,但是其实可能只有一个元素,所以最后还要判断一下

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         vector<int> v;
 5         int len=nums.size();
 6         if(len==0) return v;
 7         if(len==1){
 8             v.push_back(nums[0]);
 9             return v;
10         }
11         int a=nums[0];
12         int cnt1=1;
13         int b,cnt2=0;
14         int ok=0;
15         for(int i=1;i<len;i++){
16             if(nums[i]==a) cnt1++;
17             else if(!ok){
18                 b=nums[i];
19                 ok=1;
20                 cnt2=1;
21             }
22             else if(nums[i]==b) cnt2++;
23             else if(cnt1==0){
24                 a=nums[i];
25                 cnt1=1;
26             }
27             else if(cnt2==0){
28                 b=nums[i];
29                 cnt2=1;
30             }
31             else{
32                 cnt1--;
33                 cnt2--;
34             }
35         }
36         cnt1=0,cnt2=0;
37         for(int i=0;i<len;i++){
38             if(nums[i]==a) cnt1++;
39             if(nums[i]==b) cnt2++;
40         }
41         if(cnt1>len/3) v.push_back(a);
42         if(cnt2>len/3) v.push_back(b);
43         return v;
44     }
45 };
时间: 2024-10-09 01:32:30

leetcode-Majority Element II-229的相关文章

LeetCode &quot;Majority Element II&quot;

Similar as "Majority Element". There are at most 2 such elements, for > floor(n/3), and every non-hit element will decrease count of EACH hit element. https://leetcode.com/discuss/42769/o-n-time-and-in-o-1-space-c-solution

【LeetCode】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint

169. Majority Element &amp;&amp; 229. Majority Element II

169. Majority Element 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. Hide T

[LeetCode][JavaScript]Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. https://leetcode.com/problems/majority-element-ii/ 使用Moore voting algorithm,时间复杂度是

Majority Element &amp;&amp; Majority Element II

Majority Element https://leetcode.com/problems/majority-element/ Difficulty: Easy 查找数组的多数元素(majority element) 多数元素为数组中出现次数多于?n/2?的元素.假设数组非空且多数元素一定存在 LeetCode的解答中给出了七种思路 第一种是Brute force solution,时间复杂度为O(n2),顾名思义,遍历数组,依次判断每个元素是否为多数元素 第二种是Hash table,时间复

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj

LeetCode OJ 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

[LeetCode]Majority Element

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. Credits: Special thanks to @

LeetCode——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相