当前位置:网站首页>[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;
}
};
边栏推荐
- TPS Motion(CVPR2022)视频生成论文解读
- Matlab vector and matrix
- How can programmers improve mental internal friction?
- Distributed | practice: smoothly migrate business from MYCAT to dble
- 【Day03_0420】C语言选择题
- 递归函数中 有两个递归入口的时间复杂度
- Leetcode:336. palindrome pair
- Niuke network: TOPK problem of additive sum between two ordinal groups
- Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
- Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
猜你喜欢

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

Kingbasees SQL language reference manual of Jincang database (6. Expression)

移动web

Mysql45 talking about global lock, table lock and row lock

Implementation of PHP multitask second timer

数据库sql语言实战

光量子里程碑:6分钟内解决3854个变量问题

Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)

Introduction to three feasible schemes of grammatical generalization

二叉树的前中后序遍历——本质(每个节点都是“根”节点)
随机推荐
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
【Day_03 0420】数组中出现次数超过一半的数字
Embedded sharing collection 14
Xiao He shows his sharp corners and says hello to flutter app
Solutions to the failure of copy and paste shortcut keys
Blurring of unity pixel painting
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
【BM2 链表内指定区间反转】
TPS Motion(CVPR2022)视频生成论文解读
What is spark serialization for?
Optical quantum milestone: 3854 variable problems solved in 6 minutes
[highly available MySQL solution] centos7 configures MySQL master-slave replication
Youwei low code: Brick life cycle component life cycle
Unity2d animator cannot create transition
【Day_06 0423】不要二
Jz36 binary search tree and bidirectional linked list
VS中使用动态库
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
Matlab 向量与矩阵