当前位置:网站首页>I did it with two lines of code. As a result, my sister had a more ingenious way
I did it with two lines of code. As a result, my sister had a more ingenious way
2022-07-02 13:21:00 【Java geek Technology】

Admit defeat Online .
Since I brought my sister's learning heart up , Her enthusiasm for learning never went down . see , Come to ah fan in the morning to discuss the algorithm .
sister : Cute guy , Let me give you a question
Afan : Come on , I don't believe it can be difficult for me
sister : Given a size of n Array of , Find most of them . Most elements refer to the number of occurrences in an array greater than ⌊ n/2 ⌋ The elements of . You can assume that the array is not empty , And there are always many elements in a given array . such as : Array [3,2,3] Most elements of are 3 , Array [2,2,1,1,1,2,2] Most elements of are 2 . What about? , You can answer it ?
Afan : You really underestimate me .
Ah fan thought a little , Since most elements appear in the array more than ⌊ n/2 ⌋ , I'll put this array in order , Then take the middle value , The affirmation is that most elements , Otherwise, it does not conform to the meaning of the topic .
In this case , Two lines of code :
java Arrays.sort(nums); return nums[nums.length >> 1];
After ah fan finished writing the program , There is nothing wrong with the operation , And two lines of code is done , Fan couldn't help being proud

Afan : My little sister , I realized , Just two lines of code .
Sister looked , The result is a look of disgust : Your code is simple , But the time complexity is O(nlogn) , The space complexity is O(logn) , There should be a better solution .
Helpless, ah fan thought like this at the beginning , So I can't think of any other good way , Go and ask my sister .
sister : In fact, there is a better plan than yours , It's Moore voting . How do we usually vote ? Everyone chooses one person to write on the note , Then he began to open the paper ball to see who he chose . At first, it was assumed that everyone was 0 ticket , Then who is on the note , This person has one more vote , Finally, it depends on who has more votes . Back to our topic , Since it is mode , And the number of occurrences is greater than ⌊ n/2 ⌋ , Then we can assume that a number is the required mode , At the same time, set the number of occurrences of this number to 0 , Then compare with the following figures . If it's the same , Let's add the number of times this number appears 1 , If it's not the same , Just reduce the number 1 , When this value is reduced to 0 when , Explain that the number assumed at the beginning is not a mode , Then change the current figure , Continue to cycle . In this way, the number of occurrences of the last number must be greater than or equal to 0 Of , Otherwise, it will not meet Occurs more than ⌊ n/2 ⌋ This topic is meaningful , Last, last , Return the true mode .
Afan : You say so , It opened my mind , You wait , I'm going to realize .
java // Set the initial number of votes to 0 int count = 0 ; // First define the required mode as null Integer majorityElement = null; // Circular array for(int num : nums){ // When count by 0 when , Suppose the current number is the required mode if (count == 0){ majorityElement = num; } // When num Equal to the assumed mode , count Just add 1 count += ( num == majorityElement ) ? 1 : -1 ; } // Finally, return the true mode return majorityElement;
Ah fan made a small calculation in his heart , It is found that the time complexity is O(n) , The space complexity is O(1) , Ah fan couldn't help but give her a big finger in her heart

Let a fan admit defeat online , Will you learn
< END >
边栏推荐
- Redis数据库持久化
- PR usage skills, how to use PR to watermark?
- [opencv learning] [Canny edge detection]
- (7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
- How to modify the error of easydss on demand service sharing time?
- Lucky numbers in the [leetcode daily question] matrix
- de4000h存储安装配置
- Principle analysis of security rememberme
- Unity skframework Framework (XVI), package manager Development Kit Manager
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
猜你喜欢

解答:EasyDSS视频点播时音频是否可以设置为默认开启?

Redis数据库持久化

Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
![[opencv learning] [contour detection]](/img/96/aaec61f137e4526c2c329e6fcfa1a2.jpg)
[opencv learning] [contour detection]

国产免费数据仓库ETL调度自动化运维专家—TASKCTL
![Lucky numbers in the [leetcode daily question] matrix](/img/c8/47a22644bf8cc1f49c5668d72161b6.jpg)
Lucky numbers in the [leetcode daily question] matrix

能自动更新的万能周报模板,有手就会用!

最近公共祖先LCA的三种求法

(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
![[200 opencv routines] 100 Adaptive local noise reduction filter](/img/89/9e9b667dd28cb25af005b6028ef26c.jpg)
[200 opencv routines] 100 Adaptive local noise reduction filter
随机推荐
Everyone wants to eat a broken buffet. It's almost cold
[OpenGL] notes 29. Advanced lighting (specular highlights)
MySQL: Invalid GIS data provided to function st_ geometryfromtext
上海交大教授:何援军——包围盒(包容体/包围盒子)
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
How to modify the error of easydss on demand service sharing time?
为什么switch 的default后面要跟break?
Unity SKFramework框架(十五)、Singleton 单例
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Bridge of undirected graph
Web Foundation
[opencv learning] [image pyramid]
Download files and preview pictures
[Unity]使用GB2312,打包后程序不正常解决方案
numpy数组计算
[opencv learning] [template matching]
机器学习基础(二)——训练集和测试集的划分
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Fundamentals of face recognition (facenet)
Unity skframework framework (XX), VFX lab special effects library