LeetCode 之 Majority Element

找出数组中出现次数大于n/2次的元素。

1.先排序,处于中间n/2处的元素必然是要求的元素,道理很简单,就算把其他元素全放在前半部分或者后半部分也都会占不满的,中间的永远是majority element;

2.暴力,把每个元素出现次数记录下来,一旦大于n/2,就结束。由于每次都要与之前遍历过的元素进行比较,时间复杂度为O(n2);

3.分治,每出现两对不同的数就全部舍弃,这样到最后必然剩下的只会是majority element。但是一般实现复杂度还是比较高。巧妙实现见moore voting algorithm;

4.Moore voting algorithm,这个算法是美国一个大学教授发明的。主要思想是采用一个计数器counter和一个候选元素candidate,有两种情况:一是当counter为0时,将当前元素设为candidate,counter加1,起始这样设置秀好理解,中途这样做是因为counter如果是减到0的,那么就意味着之前的元素全部都两两相抵消了;二是当candidate与当前遍历元素相同就counter加1,不同就减1,减1的道理也是相当于两两抵消了一对不同的元素。最后的candidate就是Majority element。仔细想想这个算法,其实也就是分治法两两抵消不相同元素的一种良好实现,通过counter和candidate实现了分治法的从相邻元素的两两抵消推广到了不相邻元素的两两抵消了。牛!

代码:

 1 int majorityElement(int num[], int n) {
 2     int counter=0;
 3     int candidate;
 4     int i;
 5     for(i=0; i<n; i++)
 6     {
 7         if(counter == 0)
 8         {
 9             counter++;
10             candidate = num[i];
11         }
12         else if(num[i] == candidate)
13         {
14             counter++;
15         }
16         else
17         {
18             counter--;
19         }
20     }
21     return candidate;
22 }
时间: 2024-10-17 09:38:01

LeetCode 之 Majority Element的相关文章

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] 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(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 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 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. 题目标签:Array 忘记说了,特地回来补充,今天看完<

[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][JavaScript]Majority Element

https://leetcode.com/problems/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 majorit

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

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]