当前位置:网站首页>Leetcode skimming ---217
Leetcode skimming ---217
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given an array of integers
nums. If any value appears in the array At least twice , returntrue; If each element in the array is different from each other , returnfalse.
Input :nums = [1,2,3,1]
Output :true
Method 1 : Sort
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
};Complexity analysis
Time complexity :O(NlogN)
Spatial complexity :O(logN)
Method 2 : Hashtable
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int x: nums) {
if (s.find(x) != s.end()) {
return true;
}
s.insert(x);
}
return false;
}
};Complexity analysis
Time complexity :O(N)
Spatial complexity :O(N)
边栏推荐
- Ind wks first week
- Hands on deep learning pytorch version exercise solution - 2.4 calculus
- A complete mall system
- Leetcode刷题---278
- 20220601数学:阶乘后的零
- Automatic derivation of introduction to deep learning (pytoch)
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- 2018 y7000 upgrade hard disk + migrate and upgrade black apple
- Ut2013 learning notes
- Advantageous distinctive domain adaptation reading notes (detailed)
猜你喜欢
随机推荐
Ut2014 learning notes
Realize an online examination system from zero
Softmax 回归(PyTorch)
Judging the connectivity of undirected graphs by the method of similar Union and set search
What useful materials have I learned from when installing QT
Leetcode刷题---189
Simple real-time gesture recognition based on OpenCV (including code)
神经网络入门之预备知识(PyTorch)
侯捷——STL源码剖析 笔记
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Timo background management system
Leetcode刷题---1385
Adaptive Propagation Graph Convolutional Network
20220604 Mathematics: square root of X
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
High imitation wechat
Policy gradient Method of Deep Reinforcement learning (Part One)
ECMAScript--》 ES6语法规范 ## Day1
ECMAScript -- "ES6 syntax specification # Day1
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow









