当前位置:网站首页>Leetcode-169. most elements
Leetcode-169. most elements
2022-07-26 03:24:00 【KGundam】
Mathematical problems
Topic details
Given a size of n Array of nums , Return most of the elements . 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 .
Example 1:
Input :nums = [3,2,3]
Output :3
Example 2:
Input :nums = [2,2,1,1,1,2,2]
Output :2
Ideas :
An important point in the topic is that there must be many elements in a given array , That is, the number of occurrences in the array Greater than ⌊ n/2 ⌋ The elements of
Then this problem can be solved by Moore voting , Moore voting is traversing arrays , First set the first number to " The candidate " candidate, Then count the votes cnt’ Initialize to 1( Only his own vote ), Then walk back , If you encounter the same number , Just add tickets , Different numbers will be reduced , If the number of votes is reduced to 0 了 , Just change the next candidate , Continue traversing ( Note that the latter candidate may repeat the previous candidate , So you don't have to go back and traverse )
Then borrow the image description of a great God in the comment area about Moore voting :
Moore voting :
The core is the cost of competition .
Play a game of princes fighting for supremacy , Suppose your population is more than half of the total population , And it can ensure that every population can die one-on-one when they go out to fight . In the end, the country where people survive is victory .
That's a big scuffle , Worst of all, everyone's united against you ( Every time you choose as a counter, the number is mode ), Or other countries will attack each other ( Other numbers will be selected as the number of counters ), But as long as you don't fight inside , In the end, you're sure to win .
In the end, the rest must be our own people .
My code :
class Solution
{
public:
int majorityElement(vector<int>& nums)
{
int n = nums.size(), candidate = nums[0], cnt = 1;
// Because of initialization candidate by nums[0] therefore i From you to 1 Start traversing
for (int i = 1; i < n; ++i)
{
if (candidate == nums[i]) // If you encounter the same number
{
++cnt; // Just " Add one vote "
}
else // Encountered different number
{
--cnt; // Just " Minus one vote "
if (cnt == 0) // After the ticket reduction, if there are no tickets
{
// It means that the candidate's vote is definitely not greater than n/2 Of ( That is, not most elements )
candidate = nums[i]; // Then change the next candidate
cnt = 1; // The initial ticket is again 1
}
}
}
return candidate;
}
};
边栏推荐
猜你喜欢

How to correctly calculate the CPU utilization of kubernetes container

Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters

Hurry in!!! Write a number guessing game with dozens of lines of code based on the basic knowledge of C language

Small test (I)

There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first

使用anaconda配置gpu版本的tensorflow(30系列以下显卡)

PXE efficient batch network installation

MPLS basic experiment configuration

What are the methods of array sorting in JS

Where can Lora and nb-iot be used
随机推荐
canvas——绘制文本——饼图的修改
ELS window settings, WM_ CREATE、WM_ PAINT
What's good for starting a business with 10000 yuan? Is we media OK?
Opencv 在图像上进行标注(画框+写字)
Docker installs redis!!! (including detailed illustration of each step) actual combat
赶紧进来!!!用c语言基础知识几十行代码写一个猜数字小游戏
tensorflow中tf.Variable()函数的用法
2022-07-21 study notes of group 4 self-cultivation class (every day)
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
B2B2C多商户系统功能丰富,极易二开
2022-07-21 group 4 polymorphism
ue4如何进行静态渲染?5个步骤生成静态渲染
URDF 语法详解
DDD landing is called an advanced
QT笔记——临时的悬浮窗口
LoRa和NB-IOT可用用在哪些地方
大厂面试都面试些啥,看了不亏(一)
ELS callback function, exit message
Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
[stl] priority queue priority_ queue