当前位置:网站首页>[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
边栏推荐
- Code neat way to learn
- Complete example demonstration of creating table to page - joint table query
- Failure to deliver XID in Seata distributed transaction project
- Algèbre linéaire chapitre 1 - déterminants
- Fact final variable and final variable
- Use of multithreaded executors
- 10.Advance Next Round
- Exception handling: grab and throw model
- The problem that the page will refresh automatically after clicking the submit button on the form is solved
- Example of reentrant lock thread waiting to wake up
猜你喜欢

Why does the subscript of the array start from 0 instead of 1?

檢測技術與原理

Pytorch learning record (III): structure of neural network + using sequential and module to define the model

LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising

Generate excel template (drop-down selection, multi-level linkage)

Implementation of displaying database pictures to browser tables based on thymeleaf
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi

Automatic control (Han min version)

深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索

Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
随机推荐
container
自动控制(韩敏版)
Collections multiple parameter sorting
Chapter 4 of line generation - linear correlation of vector systems
JDBC connection database
Implementation of displaying database pictures to browser tables based on thymeleaf
Development environment EAS login license modification
Explain of MySQL optimization
Collection and map thread safety problem solving
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Class loading and classloader understanding
Substring Inversion (Easy Version)
Numpy common function table sorting of data processing
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Common sense of thread pool
Remedy after postfix becomes a spam transit station
20 excellent plug-ins recommended by idea
IO multiplexing of 09 redis