Majority Element
2015-07-29 22:25
387 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
分析:题意为在大小为n的数组中找到多数元素,多数元素出现次数大于⌊ n/2 ⌋
思路:没找到两个不同的元素就成对删除,最后剩下的一定是所求的元素(因为假设了数组非空,而且多数元素总是存在的)
实现方式:顺序遍历数组元素,前后不同则计数减一,相同则加一,在此过程中将所求元素存放到tem中
代码如下:
其他解法:
直接使用sort()函数对数组进行排序,则所求元素必定在排序后的数组正中间
亦可参照:
Moore Voting Algorithm
A brilliant and easy-to-implement algorithm! It also runs very fast, about 20ms.
Bit Manipulation
Another nice idea! The key lies in how to count the number of
[/code]
Divide and Conquer
This idea is very algorithmic. However, the implementation of it requires some careful thought about the base cases of the recursion. The base case is that when the array has only one element, then it is the majority one. Moreover, this solution is relatively slow in practice.
[/code]
Randomization
This is a really nice idea and works pretty well (16ms running time on the OJ, almost fastest among the C++ solutions). The proof is already given in the suggested solutions.
The code is as follows, randomly pick an element and see if it is the majority one.
[/code]
Hash Table
The hash-table solution is very straightforward. We maintain a mapping from each element to its number of appearances. While constructing the mapping, we update the majority element based on the max number of appearances we have seen. Notice that we do not need to construct the full mapping when we see that an element has appeared more than
The code is as follows, which should be self-explanatory.
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
分析:题意为在大小为n的数组中找到多数元素,多数元素出现次数大于⌊ n/2 ⌋
思路:没找到两个不同的元素就成对删除,最后剩下的一定是所求的元素(因为假设了数组非空,而且多数元素总是存在的)
实现方式:顺序遍历数组元素,前后不同则计数减一,相同则加一,在此过程中将所求元素存放到tem中
代码如下:
class Solution { public: int majorityElement(vector<int>& nums) { int tem=0; int count=0; for(int i=0;i<nums.size();i++){ if(count==0){ tem=nums[i]; count++; } else{ if(tem==nums[i]) count++; else count--; } } return tem; } };
其他解法:
直接使用sort()函数对数组进行排序,则所求元素必定在排序后的数组正中间
class Solution { public: int majorityElement(vector<int>& nums) { sort(nums.begin(), nums.end()); return nums[(nums.size() - 1) / 2]; } };
亦可参照:
Moore Voting Algorithm
A brilliant and easy-to-implement algorithm! It also runs very fast, about 20ms.
class Solution { public: int majorityElement(vector<int>& nums) { int major, counts = 0, n = nums.size(); for (int i = 0; i < n; i++) { if (!counts) { major = nums[i]; counts = 1; } else counts += (nums[i] == major) ? 1 : -1; } return major; } };
Bit Manipulation
Another nice idea! The key lies in how to count the number of
1's on a specific bit. Specifically, you need a
maskwith a
1on the
i-the bit and
0otherwise to get the
i-th bit of each element in
nums. The code is as follows.
class Solution { public: int majorityElement(vector<int>& nums) { int major = 0, n = nums.size(); for (int i = 0, mask = 1; i < 32; i++, mask <<= 1) { int bitCounts = 0; for (int j = 0; j < n; j++) { if (nums[j] & mask) bitCounts++; if (bitCounts > n / 2) { major |= mask; break; } } } return major; } };
[/code]
Divide and Conquer
This idea is very algorithmic. However, the implementation of it requires some careful thought about the base cases of the recursion. The base case is that when the array has only one element, then it is the majority one. Moreover, this solution is relatively slow in practice.
class Solution { public: int majorityElement(vector<int>& nums) { return majority(nums, 0, nums.size() - 1); } private: int majority(vector<int>& nums, int left, int right) { if (left == right) return nums[left]; int mid = left + ((right - left) >> 1); int lm = majority(nums, left, mid); int rm = majority(nums, mid + 1, right); if (lm == rm) return lm; return counts(nums, lm) > counts(nums, rm) ? lm : rm; } int counts(vector<int>& nums, int elem) { int cnt = 0; for (int i = 0; i < int(nums.size()); i++) if (nums[i] == elem) cnt++; return cnt; } };
[/code]
Randomization
This is a really nice idea and works pretty well (16ms running time on the OJ, almost fastest among the C++ solutions). The proof is already given in the suggested solutions.
The code is as follows, randomly pick an element and see if it is the majority one.
class Solution { public: int majorityElement(vector<int>& nums) { int n = nums.size(); srand(unsigned(time(NULL))); while (true) { int idx = rand() % n; int candidate = nums[idx]; int counts = 0; for (int i = 0; i < n; i++) if (nums[i] == candidate) counts++; if (counts > n / 2) return candidate; } } };
[/code]
Hash Table
The hash-table solution is very straightforward. We maintain a mapping from each element to its number of appearances. While constructing the mapping, we update the majority element based on the max number of appearances we have seen. Notice that we do not need to construct the full mapping when we see that an element has appeared more than
n / 2times.
The code is as follows, which should be self-explanatory.
class Solution { public: int majorityElement(vector<int>& nums) { unordered_map<int, int> counts; int n = nums.size(); for (int i = 0; i < n; i++) if (++counts[nums[i]] > n / 2) return nums[i]; } };
相关文章推荐
- font-size属性的继承
- 数据结构和算法学习笔记-3
- HTMLParser介绍
- Deep Learning笔记1
- JS DOM编程艺术——JS综合实例—— JS学习笔记2015-7-29(第92天)
- iOS 隐藏状态栏
- [Android优化进阶] 提高ListView性能的技巧
- iOS"Request failed: unacceptable content-type: text/html"
- Andriod实现刮刮卡的效果
- shell脚本学习知识点(二)
- hd1232 修路最少(并查集问题)
- CSS基础篇
- [Leetcode] Bitwise And of numbers range
- Cordova/Phonegap Android中Java调用JavaScript
- CF_288B_PoloThePenguinAndHouses
- js事件委托
- android判断EditText输入的数字、中文还是字母方法
- sqlit3的使用
- 给CS专业大学生的推荐书籍
- Android中Activity中与ListActivity中使用listview区别