当前位置:网站首页>[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
边栏推荐
- Common programming records - parser = argparse ArgumentParser()
- POJ - 2955 brackets interval DP
- Integration and induction of knowledge points of automatic control principle (Han min version)
- 治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
- Graphic numpy array matrix
- 12. Monkeys climb mountains
- lambda expressions
- PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
- Filebrowser realizes private network disk
- 10.Advance Next Round
猜你喜欢
Linear algebra Chapter 2 - matrices and their operations
The bottom implementation principle of thread - static agent mode
線性代數第一章-行列式
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Why does the subscript of the array start from 0 instead of 1?
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
In depth understanding of the relationship between dncblevel and noise denoising in the paper
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
随机推荐
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
Contrôle automatique (version Han min)
线性代数第三章-矩阵的初等变换与线性方程组
Pytoch -- data loading and processing
JDBC operation transaction
In depth source code analysis servlet first program
Customized communication between threads (reentrantlock)
Example of reentrant lock thread waiting to wake up
Use of multithreaded executors
Example of ticket selling with reentrant lock
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
Addition, deletion, modification and query of MySQL table
Solution record of slow access speed of SMB service in redhat6
On traversal of binary tree
線性代數第一章-行列式
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
1. Calculate a + B
Custom exception class
Illustrate the significance of hashcode