当前位置:网站首页>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 >
边栏推荐
- Post order traversal sequence of 24 binary search tree of sword finger offer
- 国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
- Unity skframework framework (XX), VFX lab special effects library
- de4000h存储安装配置
- West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
- What are eNB, EPC and PGW?
- What are eNB, EPC and PGW?
- Explanation of 34 common terms on the Internet
- 每日一题:1175.质数排列
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
猜你喜欢

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
![[200 opencv routines] 100 Adaptive local noise reduction filter](/img/89/9e9b667dd28cb25af005b6028ef26c.jpg)
[200 opencv routines] 100 Adaptive local noise reduction filter

How can attribute mapping of entity classes be without it?

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six

三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难

2022零代码/低代码开发白皮书【伙伴云出品】附下载

leetcode621. 任务调度器

Japan bet on national luck: Web3.0, anyway, is not the first time to fail!

Unity SKFramework框架(二十)、VFX Lab 特效库
随机推荐
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
自主可控三维云CAD:CrownCAD赋能企业创新设计
leetcode621. 任务调度器
How to modify the error of easydss on demand service sharing time?
Bridge of undirected graph
Everyone wants to eat a broken buffet. It's almost cold
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
Explain in detail the process of realizing Chinese text classification by CNN
Can automatically update the universal weekly report template, you can use it with your hand!
Jerry's watch gets the default ringtone selection list [article]
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
moon
MySQL: Invalid GIS data provided to function st_ geometryfromtext
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
【OpenGL】笔记二十九、高级光照(镜面高光)
Unity SKFramework框架(二十)、VFX Lab 特效库
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Ali on three sides, it's really difficult to successfully get the offer rated P7
机器学习基础(二)——训练集和测试集的划分