本文主要是介绍leetcode169~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.
public class MajorityElement {//使用集合public int majorityElement2(int[] nums) {int len = nums.length;Map<Integer,Integer> map = new HashMap<>();for(int i=0;i<nums.length;i++) {map.put(nums[i], map.getOrDefault(nums[i], 0)+1);if(map.get(nums[i])>len/2) {return nums[i];}}return 0;}//时间O(n) 空间O(1)//思想:一个变量记录最大出现次数,如果后续的比较不相等,则减少,为0时重新赋值给新值(这种做法主要原因在于假设肯定存在这样的值>n/2)public int majorityElement(int[] nums) {int count = 0;int major = 0;for(int i=0;i<nums.length;i++) {if(count==0) {major = nums[i];count++;} else if(major==nums[i]){count++;} else {count--;}}return major;}
}
这篇关于leetcode169~Majority Element的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!