当前位置:网站首页>[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;
}
};
边栏推荐
- Leetcode:741. picking cherries
- Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
- “子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- 【Day05_0422】C语言选择题
- Using dynamic libraries in VS
猜你喜欢

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)

Widget is everything, widget introduction

What is spark serialization for?

Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022

Acquisition of bidding information

unity 像素画导入模糊问题

Docking wechat payment (II) unified order API

Introduction of four redis cluster schemes + comparison of advantages and disadvantages
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism
随机推荐
What is spark serialization for?
Kingbasees SQL language reference manual of Jincang database (6. Expression)
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
MySQL multi table query introduction classic case
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism
【Day02_0419】C语言选择题
【Day_04 0421】计算糖果
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
Byte interview question - judge whether a tree is a balanced binary tree
Flex layout
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
Should we test the Dao layer?
卸载手机自带APP的操作步骤
K. Link with Bracket Sequence I dp
【Day_05 0422】连续最大和
Convolutional neural network (II) - deep convolutional network: case study
Etcd database source code analysis - cluster membership changes log
Solutions to the failure of copy and paste shortcut keys
Acquisition of bidding information