majority 求众数系列题目
作者:互联网
169. Majority ElementGiven an array
nums
of size n
, return the majority element.The majority element is the element that appears more than ⌊n / 2⌋
times. You may assume that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3] Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2] Output: 2
Constraints:
n == nums.length
1 <= n <= 5 * 104
-231 <= nums[i] <= 231 - 1
class Solution { public int majorityElement(int[] nums) { //排序 Arrays.sort(nums); //排序后找中位数肯定是那个众数,因为他出现的次数超过了1/2 return nums[nums.length/2]; } }
Could you solve the problem in linear time and in
O(1)
space?
class Solution { public int majorityElement(int[] nums) { int curr = 0; int count = 0; for(int num:nums){ if(count==0) curr = num; if(curr==num) count++; else count--; } return curr; } }
229. Majority Element II
Given an integer array of size n
, find all elements that appear more than ⌊ n/3 ⌋
times.
Follow-up: Could you solve the problem in linear time and in O(1) space?
Example 1:
Input: nums = [3,2,3] Output: [3]
Example 2:
Input: nums = [1] Output: [1]
Example 3:
Input: nums = [1,2] Output: [1,2]
Constraints:
1 <= nums.length <= 5 * 104
-109 <= nums[i] <= 109
class Solution { public List<Integer> majorityElement(int[] nums) { //该题最多存在两个众数,因为每个众数出现频率要超过1/3 int first=0,firstCount = 0; int second=0,secondCount = 0; List<Integer> result = new ArrayList(); //第一次遍历进行摩尔投票,筛选出两个数字 for(int num:nums){ if(num==first) firstCount++; else if(num==second) secondCount++; //如果哪个count为0,用当前值替换 else if(firstCount==0){ first = num; firstCount++; } else if(secondCount==0){ second = num; secondCount++; } //如果两个都不是,两个都-- else{ firstCount--; secondCount--; } } //第二次便利,校验是否大于1/3 firstCount=0; secondCount=0; for(int num:nums){ if(num==first) firstCount++; else if(num==second) secondCount++; } //将大于1/3的数字放入结果集 if(firstCount>nums.length/3) result.add(first); if(secondCount>nums.length/3) result.add(second); return result; } }48 · Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k
of the size of the array.
There is only one majority number in the array.
Example 1:
Input:
nums = [3,1,2,3,2,3,3,4,4,4]
k = 3
Output:
3
Explanation:
3 appeared four times, more than 10 / 3.
Example 2:
Input:
nums = [1,1,2]
k = 3
Output:
1
Explanation:
1 appeared two times, more than 3 / 3.
public class Solution { /** * @param nums: A list of integers * @param k: An integer * @return: The majority number */ public int majorityNumber(List<Integer> nums, int k) { Map<Integer,Integer> map = new HashMap(); for(int num:nums){ if(map.containsKey(num)) map.put(num,map.get(num)+1); else{ if(map.size()<k){ map.put(num,1); } else{ List<Integer> list = new ArrayList(); for(int key:map.keySet()){ map.put(key,map.get(key)-1); if(map.get(key)==0) list.add(key); } for(int ele:list){ map.remove(ele); } } } } for(int key:map.keySet()) map.put(key,0); for(int num:nums){ if(map.containsKey(num)) { int count = map.get(num); map.put(num,++count); if(count>nums.size()/k) return num; } } return 0; } }
标签:map,题目,nums,int,secondCount,++,majority,num,众数 来源: https://www.cnblogs.com/cynrjy/p/15335579.html