this is a question from leetcode:
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.
If you want to get the best solution, then you must analyst and make use of all the information provided in the problem.
question analysis:
the key hint is that the majority element always exist. So that means that the count of majority element is great than the count of all other element, so the
count(majority) - count(others) > 0.
below is the best solution:
public int majorityElement(int[] nums) {
int result = nums[0];
int count = 1;
for(int i = 1; i < nums.length; i++){
if(result == nums[i]){
count++;
}else{
count--;
}
if(count == 0){
result = nums[i];
count++;
}
}
return result;
}
分享到:
相关推荐
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 ...
java郑 java_leetcode题解之Online Majority Element In Subarray.java
javascript js_leetcode题解之169-majority-element.js
c c语言_leetcode题解之0229_majority_element_ii
python python_leetcode题解之229_Majority_Element_II.py
Find a majority element in an array of size 'n'3. Find the number occuring odd number of times in a given array of size 'n'4. Algorithm to reverse an array5. Algorithm to rotate array of size 'n' by ...
finding a majority among n votes.pdffinding a majority among n votes.pdffinding a majority among n votes.pdffinding a majority among n votes.pdfv
《K-majority聚类算法详解》 在数据挖掘与机器学习领域,聚类是一种重要的无监督学习方法,其中,K-means是最常见的聚类算法之一。然而,针对特定的数据类型和场景,K-means的欧式距离计算方式可能并不适用。于是,...
寻找多数元素。用递归算法MAJORITY实现多数的寻找,其中调用candidate(m)函数。
This applies for a uniform distribution of search items but if it is known that some few items are much more likely to be sought for than the majority then a linear search with the list ordered so ...
Whether you are capturing requirements, developing a software architecture, designing the implementation, or trying to understand an existing system, this is the book for you.The majority of the book...
The ALO algorithm also finds superior optimal designs for the majority of classical engineering problems employed, showing that this algorithm has merits in solving constrained problems with diverse ...
标题中的“Three-input-Majority-Voter”指的是一个三输入的多数表决逻辑电路,也被称为“三人表决器”。在电子工程和计算机科学中,这样的电路或系统被设计用来基于三个输入信号(通常代表三位用户的投票)来产生一...
用于处理标签噪声的matlab代码,label noise,集成学习
非常好用的,直接可以用于。net下的任何程序集的保密Programs written for the CLR reside ... Rather, the majority of a CLR module's contents are stored as opaque data in the.text section of the PE/COFF file.
Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array Shortest Unsorted Continuous Subarray Rotate Image 宝石与石头Jewels and Stones Kids With the Greatest Number of Candies 美团...
An “activity” is an element of work performed during the course of a project [PMI, 2000]. It has an output and leads towards an outcome. Such an output can either be an input to another activity or ...