当前位置:网站首页>[leetcode169] most elements
[leetcode169] most elements
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
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 .
The array is not empty , And there are always many elements in a given array .
Example 1:
Input :[3,2,3]
Output :3
Example 2:
Input :[2,2,1,1,1,2,2]
Output :2
Their thinking
There are two solutions to this problem , But the time complexity of the two methods is one order of magnitude different , Readers should compare the differences between the two methods , Especially the subtlety of the second method .
Method 1: Using the idea of sorting
This idea is what most people can think of , It is also the one with the least number of lines of code , But it will take advantage of STL Provided in the standard template library sort() function ,
For beginners, it is recommended to use this method directly API Of , Will limit your thinking , To do algorithm problems is to cultivate ideas . This solution time
The complexity is about O(nlogn).
int majorityElement(vector<int>& nums) {
sort(nums.begin,nums.end);
return nums[nums.size()/2];
}
Method 2: Start with the first number count=1, In case of the same, add 1, In case of different, reduce 1, Reduced to 0 Just change the number and start counting , Always find the most . You'd better simulate it manually , Experience the subtlety of this solution , Deepen the impression . Time complexity O(n)
int majorityElement(vector<int>& nums) {
int count=1;
int flag=nums[0];
for(int i=1;i<nums.size();i++)
{
if(flag==nums[i])
count++;
else{
count--;
if(count==0)
flag=nums[i+1];
}
}
return flag;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012309.html
边栏推荐
- PHP processing JSON_ Decode() parses JSON stringify
- Kibana search syntax
- Remedy after postfix becomes a spam transit station
- 11.a==b?
- Viewer: introduce MySQL date function
- PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
- Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
- CONDA virtual environment management (create, delete, clone, rename, export and import)
- Framework analysis 2 Source code - login authentication
- Class loading and classloader understanding
猜你喜欢
Framework analysis 1 Introduction to system architecture
Chapter 4 of line generation - linear correlation of vector systems
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
Best practices for MySQL storage time
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
Addition, deletion, modification and query of MySQL table
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
随机推荐
Code neat way to learn
10.Advance Next Round
Generate excel template (drop-down selection, multi-level linkage)
Contrôle automatique (version Han min)
程序设计训练
How to grow at work
Best practices for MySQL storage time
Custom exception class
2. Devops sonar installation
Why does the subscript of the array start from 0 instead of 1?
Understanding and installing MySQL
Exception handling: grab and throw model
Common sense of thread pool
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Integration and induction of knowledge points of automatic control principle (Han min version)
1. Calculate a + B
Fact final variable and final variable
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
JDBC tool class encapsulation