LeetCode OJ:Majority Element II(主元素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.

求主元素,这次的是大于sz/3就算是主元素,可以分为两轮查找,第一轮先查找元素数目较多的两个元素(可能不属于主元素),第二次再遍历来查找上面两个元素是否符合条件,代码如下:

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         int m1, m2;
 5         int count1, count2;
 6         vector<int> ret;
 7         int sz = nums.size();
 8         if(!sz) return ret;
 9         m1 = nums[0];
10         m2 = 0;
11         count1 = 1;
12         count2 = 0;
13         for(int i = 1; i < sz; ++i){
14             if(m1 == nums[i])
15                 count1++;
16             else if(m2 == nums[i])
17                 count2++;
18             else if(count1 == 0){
19                 count1++;
20                 m1 = nums[i];
21             }else if(count2 == 0){
22                 count2++;
23                 m2 = nums[i];
24             }else{
25                 count1--;
26                 count2--;
27             }
28         }
29         count1 = count2 = 0;
30         for(int i = 0; i < sz; ++i){
31             if(nums[i] == m1) ++count1;
32             if(nums[i] == m2) ++count2;
33         }
34         if(count1 > sz/3) ret.push_back(m1);
35         if(m1 != m2)
36             if(count2 > sz/3) ret.push_back(m2);
37         return ret;
38     }
39 };
时间: 2024-11-03 21:23:40

LeetCode OJ:Majority Element II(主元素II)的相关文章

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. 求主元素:包含n个元素的数组中,如果一个元素的出现次数大

LeetCode Problem: 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. 思路1:Moore voting algorith

[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

主元素 II

主元素 II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O(1). 嗯.. 百度了一下. 主元素可能有两个,于是设置两个当前主元素.遍历nums,如果和某个当前住元素相等,则计数加一.如果都不相等 计数减一,若减后计数小于等于0,则将对应的当前住元素更换. 最后需要验证,应为当前主元素只有一个,候选有两个. lintcode上

Leetcode problem-169 Majority Element 题解

Leetcode Problem-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 th

[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,时间复杂度是

leetcode——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 Tags: Divide and Conquer

LeetCode(7): Majority Element

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. 题意:找出给定数组中的

leetcode 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. 思路: Find k different element