当前位置:网站首页>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)
边栏推荐
- Leetcode-404:左叶子之和
- Ut2017 learning notes
- 2018 Lenovo y7000 black apple external display scheme
- Leetcode - the k-th element in 703 data flow (design priority queue)
- Leetcode刷题---977
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
- 20220606数学:分数到小数
- Rewrite Boston house price forecast task (using paddlepaddlepaddle)
- 20220531 Mathematics: Happy numbers
- Leetcode刷题---202
猜你喜欢
Knowledge map enhancement recommendation based on joint non sampling learning
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
mysql5.7安装和配置教程(图文超详细版)
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
波士顿房价预测(TensorFlow2.9实践)
Raspberry pie 4B installs yolov5 to achieve real-time target detection
An open source OA office automation system
Secure in mysql8.0 under Windows_ file_ Priv is null solution
七、MySQL之数据定义语言(二)
随机推荐
Boston house price forecast (tensorflow2.9 practice)
Are there any other high imitation projects
Simple real-time gesture recognition based on OpenCV (including code)
Policy gradient Method of Deep Reinforcement learning (Part One)
Inverse code of string (Jilin University postgraduate entrance examination question)
Leetcode刷题---283
Ut2011 learning notes
Ind yff first week
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Ut2016 learning notes
权重衰退(PyTorch)
QT creator uses OpenCV Pro add
Model evaluation and selection
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
熵值法求权重
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
侯捷——STL源码剖析 笔记
Adaptive Propagation Graph Convolutional Network
Timo background management system