[LeetCode]Majority Element
2016-12-09 17:40
232 查看
Question
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.
本题难度Easy。有3种算法分别是:哈希法、排序法、投票法(最巧妙)
【题意】
You may assume that the array is non-empty and the majority element always exist in the array.
1、哈希法
【复杂度】
时间 O(N) 空间 O(N)
【思路】
在遍历数组的过程中,用一个哈希表记录每个数出现过的次数,如果该次数大于一半,则说明是众数。
【代码】
2、排序法
【复杂度】
时间 O(NlogN) 空间 O(1)
【思路】
将数组排序,这时候数组最中间的数肯定是众数。
【代码】
3、投票法
【复杂度】
时间 O(N) 空间 O(1)
【思路】
设一个投票变量
【代码】
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.
本题难度Easy。有3种算法分别是:哈希法、排序法、投票法(最巧妙)
【题意】
You may assume that the array is non-empty and the majority element always exist in the array.
1、哈希法
【复杂度】
时间 O(N) 空间 O(N)
【思路】
在遍历数组的过程中,用一个哈希表记录每个数出现过的次数,如果该次数大于一半,则说明是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require int size=nums.length; Map<Integer,Integer> map=new HashMap<>(); //invariant for(int n:nums){ if(map.containsKey(n)){ if(map.get(n)==size/2)return n; map.put(n,map.get(n)+1); }else map.put(n,1); } //ensure return nums[0];//这是针对只有1个元素情况 } }
2、排序法
【复杂度】
时间 O(NlogN) 空间 O(1)
【思路】
将数组排序,这时候数组最中间的数肯定是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require Arrays.sort(nums); //ensure return nums[nums.length/2]; } }
3、投票法
【复杂度】
时间 O(N) 空间 O(1)
【思路】
设一个投票变量
candidate,还有一个计数变量
cnt,开始遍历数组。如果新数和
candidate一样,那么
cnt加上1;否则,如果
cnt==1,则将
candidate更新为这个新的数,如果
cnt>1,则
cnt减去1。因为每一对不一样的数都会互相消去,最后留下来的
candidate就是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require int size=nums.length; int candidate=nums[0],cnt=1; //invariant for(int i=1;i<size;i++){ int n=nums[i]; if(n==candidate)cnt++; else if(cnt==1)candidate=n; else cnt--; } //ensure return candidate; } }
参考
[Leetcode] Majority Element 众数相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode刷题,没想到这么难搞!
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解