169. Majority Element
来源:互联网 发布:mac .git文件夹 编辑:程序博客网 时间:2024/05/16 09:54
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 @ts for adding this problem and creating all test cases.
这里要隆重介绍一下O(n)复杂度的 Boyer–Moore majority vote algorithm。这是一个专门统计序列中出现频率最高的字符的算法,伪代码如下:
Initialize an element m and a counter i with i = 0For each element x of the input sequence:If i = 0, then assign m = x and i = 1else if m = x, then assign i = i + 1else assign i = i − 1Return m
转换成这道题 java 的解法就是:
public class Solution { public int majorityElement(int[] nums) { int i = 0, count = 0, majority = 0; while (i < nums.length) { if (count == 0) { majority = nums[i]; count ++; } else if (majority == nums[i]) { count ++; } else { count --; } i++; } return majority; }}
实在是巧妙。其实这道题还有很多其他的解法,比如sorting, hashmap, moore voting, bit manipulation。具体的代码实现如下:
// Sortingpublic int majorityElement1(int[] nums) { Arrays.sort(nums); return nums[nums.length/2];}// Hashtable public int majorityElement2(int[] nums) { Map<Integer, Integer> myMap = new HashMap<Integer, Integer>(); //Hashtable<Integer, Integer> myMap = new Hashtable<Integer, Integer>(); int ret=0; for (int num: nums) { if (!myMap.containsKey(num)) myMap.put(num, 1); else myMap.put(num, myMap.get(num)+1); if (myMap.get(num)>nums.length/2) { ret = num; break; } } return ret;}// Moore voting algorithmpublic int majorityElement3(int[] nums) { int count=0, ret = 0; for (int num: nums) { if (count==0) ret = num; if (num!=ret) count--; else count++; } return ret;}// Bit manipulation public int majorityElement(int[] nums) { int[] bit = new int[32]; for (int num: nums) for (int i=0; i<32; i++) if ((num>>(31-i) & 1) == 1) bit[i]++; int ret=0; for (int i=0; i<32; i++) { bit[i]=bit[i]>nums.length/2?1:0; ret += bit[i]*(1<<(31-i)); } return ret;}最后一个 Bit manipulation 非常巧妙,因为序列中的数都是 int 型(32位)。分别统计序列中每一个数每一位出现的次数,大于n/2的位即属于出现次数大于n/2的那个数,把属于的位合并,得到最终结果,思想是Divide and Conquer。
0 0
- 169.Majority Element && 229. Majority Element II
- [LeetCode]169.Majority Element
- LeetCode 169.Majority Element
- 【LeetCode】169.Majority Element
- LeetCode 169. Majority Element
- LeetCode 169. Majority Element
- 169.Majority Element
- [leetcode] 169.Majority Element
- 169. Majority Element
- Leetcode 169. Majority Element
- 【LeetCode】169. Majority Element
- [LeetCode]169. Majority Element
- 169. Majority Element LeetCode
- 169. Majority Element
- 169. Majority Element
- LeetCode 169. Majority Element
- LeetCode 169. Majority Element
- Leetcode 169. Majority Element
- IO BufferedInputStream
- IO BufferedOutputStream
- Leetcode 117. Populating Next Right Pointers in Each Node II (Hard) (cpp)
- exercise 5 多变量
- 使用buffer拷贝文件
- 169. Majority Element
- Array126FindPeakElement
- nginx事件模块之客户端连接与超时管理
- jsp页面取得物理路径的方法
- Array216CombinationSum3
- Ogre1.9SDK学习笔记(一)序
- 如何利用遗传算法进行自变量降维(代码部分)
- FFMPEG系列课程(一)打开视频解码器
- 283. Move Zeroes