当前位置:网站首页>[leetcode217] there are duplicate elements
[leetcode217] there are duplicate elements
2022-04-23 06:23:00 【Don't steal my energy】
There are duplicate elements
Q: Give you an array of integers nums . If any value appears in the array At least twice , return true ; If each element in the array is different from each other , return false .
Example
Input :nums = [1,2,3,1]
Output :true
Input :nums = [1,2,3,4]
Output :false
Input :nums = [1,1,1,3,3,4,3,2,4,2]
Output :true
Two ways of thinking , One is to use the method of sorting , Time complexity O(nlogn); The second is hash table , Time complexity O(N)
As an algorithm beginner , We are not just satisfied with “ adopt ”, It's comparing ideas , Extract its essence , Go to its bad place . Learn from others' strong points and close the gap , In this way, we can improve ourselves . The problem is not just to solve , We should also solve the problem in an efficient way , Only our way of thinking has changed , We will improve .
1. Sort
bool containsDuplicate(vector<int>& nums) {
The idea of sorting Time complexity O(nlogn)
sort(nums.begin(),nums.end());
if(nums.size()<2)// There is only one element or no element
return false;
for(int i=0;i<=nums.size()-2;i++)// Other situations
if(nums[i]==nums[i+1])
return false;
return true;
}
2. Hashtable
bool containsDuplicate_plus(vector<int>& nums) {
// Using a hash table O(n)
unordered_set<int>s;
for(auto x:nums){
if(s.count(x)==0)
s.insert(x);
else
return true;
}
return false;
}
The latter is a way of exchanging space for time .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012258.html
边栏推荐
- Framework analysis 1 Introduction to system architecture
- 8. Integer Decomposition
- List segmentation best practices
- Contrôle automatique (version Han min)
- 线性代数第一章-行列式
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- Framework analysis 2 Source code - login authentication
- In depth source code analysis servlet first program
- PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
- Customized communication between threads (reentrantlock)
猜你喜欢

线性代数第一章-行列式

Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package

A general U-shaped transformer for image restoration

Preparedstatement prevents SQL injection
![去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots](/img/fd/84df62c88fe90a73294886642036a0.png)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots

PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码

container

Chapter 4 of line generation - linear correlation of vector systems

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

Pyqy5 learning (2): qmainwindow + QWidget + qlabel
随机推荐
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
Framework analysis 2 Source code - login authentication
Filebrowser realizes private network disk
MySQL basic madness theory
Example of ticket selling with reentrant lock
2. Devops sonar installation
POI and easyexcel exercises
Installation and usage skills of idea
Solution record of slow access speed of SMB service in redhat6
LockSupport. Park and unpark, wait and notify
10.Advance Next Round
8. Integer Decomposition
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
线性代数第三章-矩阵的初等变换与线性方程组
Linear algebra Chapter 1 - determinant
Viewer: introduce MySQL date function
C language file operation
线性代数第一章-行列式