当前位置:网站首页>[day_030420] numbers that appear more than half of the time in the array
[day_030420] numbers that appear more than half of the time in the array
2022-07-26 06:11:00 【On the Bank of Anhe Bridge】
A number that appears more than half the times in an array
Title source
Cattle from : A number that appears more than half the times in an array
Title Description
Give a length of n Array of , There is a number in an array that appears more than half the length of the array , Please find out the number .
For example, enter a length of 9 Array of [1,2,3,2,2,2,5,4,2]. Because of the number 2 Appears in the array 5 Time , More than half the length of the array , So output 2.
Data range :n≤50000, The value of the element in the array 0≤val≤10000
requirement : Spatial complexity :O(1), Time complexity O(n)
Input description
The first line of input is a positive integer n(1 ≤ n ≤ 10^5)
The second line includes 3*n It's an integer a_i(1 ≤ a_i ≤ 10^9), Represents the level value of each contestant .
Example 1
Input
[1,2,3,2,2,2,5,4,2]
Return value
2
Example 2
Input
[3,3,3,3,2,2,2]
Return value
3
Example 3
Input
[1]
Return value
1
Thought analysis
Solution 1
- Offset method
- If two numbers are not equal , Just eliminate these two numbers , In the worst case , Eliminate one mode and one non mode at a time , So if there are modes , The last number left must be the mode
- Define a ret Save the current mode , Definition times Indicates the number of occurrences
- Traversal array , The current array value is the same as ret Equal increases times value , Inequality decreases times value ,times The value is 0 Replace with the value of the current array element ret
Solution 2
- Sort the array
- Find the number in the middle of the array , Because if a number exists in the array more than half times , Then the number in the middle of the array after sorting must be this number
Code display
Solution 1
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int times = 1;
int ret = numbers[0];// Definition ret For the first element of the array , If an array has only one element , Can return directly ret
int sz = numbers.size();
for (int i = 1; i < sz; i++)
{
// At present times Value is not 0
if (times != 0)
{
if (numbers[i] != ret)
{
times--;
}
else
{
times++;
}
}
// At present times The value is 0, The original ret Has been offset by unequal values
else
{
// Assign the value of the current array to ret,tims Reset to 1
ret = numbers[i];
times = 1;
}
}
return ret;
}
};
Solution 2
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
sort(numbers.begin(),numbers.end());
int sz=numbers.size();
int max=numbers[sz/2];// The middle of the array
return max;
}
};
边栏推荐
- Recursive processing - subproblem
- Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
- CV (1)- Introduction
- Embedded sharing collection 15
- 【Day02_0419】C语言选择题
- 【2023杰理科技提前批笔试题】~ 题目及参考答案
- L. Link with Level Editor I dp
- 【Day_07 0425】合法括号序列判断
- 漫谈软件缺陷管理的实践
- Easycvr video square channel display and video access full screen display style problem repair
猜你喜欢

Introduction to three feasible schemes of grammatical generalization

Interview difficulties: difficulties in implementing distributed session, this is enough!

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

Intelligent fire protection application based on fire GIS system

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
C language explanation series - comprehensive exercises, guessing numbers games

Convolutional neural network (IV) - special applications: face recognition and neural style transformation

Redis sentinel cluster setup

JDBC streaming query and cursor query

Distributed | practice: smoothly migrate business from MYCAT to dble
随机推荐
VRRP protocol and experimental configuration
Interpretation of TPS motion (cvpr2022) video generation paper
Recursive processing - subproblem
The number of weeks of Oracle last year and this year, with the start time and end time
Interview difficulties: difficulties in implementing distributed session, this is enough!
Latex同时合并表格的多行多列
JDBC streaming query and cursor query
Byte interview question - judge whether a tree is a balanced binary tree
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
【Day_04 0421】进制转换
Is the transaction in mysql45 isolated or not?
【Day03_0420】C语言选择题
[highly available MySQL solution] centos7 configures MySQL master-slave replication
基于消防GIS系统的智慧消防应用
Understanding the mathematical essence of machine learning
Acquisition of bidding information
2022年下半年系统集成项目管理工程师(软考中级)报名条件
【Day_05 0422】连续最大和
Youwei low code: Brick life cycle component life cycle
Modifiers should be declared in the correct order 修饰符应按正确的顺序声明