当前位置:网站首页>[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
边栏推荐
- The user name and password of users in the domain accessing the samba server outside the domain are wrong
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
- St table template
- Use of multithreaded executors
- The bottom implementation principle of thread - static agent mode
- Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
- A general U-shaped transformer for image restoration
- ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
- 10.Advance Next Round
猜你喜欢

Linear algebra Chapter 1 - determinant
![去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention](/img/4e/1a51636853d11544e6f5c37a588730.png)
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
![去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots](/img/fd/84df62c88fe90a73294886642036a0.png)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots

20 excellent plug-ins recommended by idea

Gaussian processes of sklearn

Automatic control (Han min version)

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

深度学习基础——简单了解meta learning(来自李宏毅课程笔记)

A sharp tool to improve work efficiency
随机推荐
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Protected (members modified by protected are visible to this package and its subclasses)
List segmentation best practices
Complete example demonstration of creating table to page - joint table query
MySQL occasional Caton
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
2. Devops sonar installation
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Kalman filter and inertial integrated navigation
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
Collection and map thread safety problem solving
container
4. Print form
Understanding and installing MySQL
Explain of MySQL optimization
St table template
Exception handling: grab and throw model
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
6.Reversal