当前位置:网站首页>Most elements leetcode
Most elements leetcode
2022-06-13 00:56:00 【-LM-】
Most elements

Method 1 ( Hashtable )
Use a hash table to store each element and the number of occurrences of the element , When the hash table already has this element , Take the value and add one to the hash table , Finally, you only need to traverse the hash table , Find a key with a value greater than or equal to half the length of the array , Return to .
public static int majorityElement(int[] nums) {
Map<Integer,Integer > map = new HashMap<Integer,Integer >();
for(int i=0;i<nums.length;i++){
if(!map.containsKey(nums[i])){
map.put(nums[i],1);
}else{
map.put(nums[i],map.get(nums[i])+1);
}
}
for(Map.Entry<Integer,Integer> entry:map.entrySet()){
if(entry.getValue()>nums.length/2)
return entry.getKey();
}
return 0;
}
Method 2 ( Sort )
By sorting the array from small to large , Find the element in the middle of the array , The number of this element must be greater than or equal to half the length of the array .
public static int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
Method 3 ( Moore voting )
step :
1、 Maintain a candidate mode candidate And the number of times it appears count. At the beginning candidate It can be any value ,count by 0.
2、 Traversal array nums All elements in , For each element x, In judging x Before , If count The value of is 0, First the x Value assigned to candidate, Subsequent judgment x.
3、 If x And candidate equal , Then the counter count The value of the increase 1; If x And candidate Unequal , Then the counter count The value of is reduced 1.
4、 After traversal ,candidate That is, the mode of the entire array .
public static int majorityElement(int[] nums) {
int count = 0;
Integer candidate=null;
for(int num:nums){
if(count==0)
candidate=num;
if(num==candidate) count++;
else count--;
}
return candidate;
}
边栏推荐
- [003] embedded learning: creating project templates - using stm32cubemx
- Jenkins持续集成操作
- 三角波与三角波卷积
- Today's sleep quality record 74 points
- Introduction to ROS from introduction to mastery (zero) tutorial
- 单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
- 五篇经典好文,值得一看(2)
- 3623. Merge two ordered arrays
- 深度学习模型剪枝
- Kotlin coroutine suspend function suspend keyword
猜你喜欢

Canvas game 2048 free map size

What is the difference between pytorch and tensorflow?

Common skills for quantitative investment - drawing 2: drawing the moving average

Development notes of Mongoose

Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing

Self use notes for problem brushing learning

Aof persistence

How to handle different types of data

Unity calls alertdialog

What is dummy change?
随机推荐
Kotlin coroutine withcontext switch thread
MySQL query table field information
ImportError: cannot import name &#039;get_ora_doc&#039; from partially initialized module
Maybe we can figure out the essence of the Internet after the dust falls
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
Go simple read database
MySQL locates the position of the character in the string String substitution
Canvas game 2048 free map size
Liu Hui and introduction to nine chapter arithmetic and island arithmetic
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
Paper reading and sharing
What is meebits? A brief explanation
Common skills for quantitative investment - drawing 3: drawing the golden section line
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Mysql database password modification
How many rounds of deep learning training? How many iterations?
Kotlin 协程挂起函数 suspend 关键字
Win10 home vs pro vs enterprise vs enterprise LTSC
How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
刘徽与《九章算术》《海岛算经》简介