solve: 0217 Contains Duplicate (C++ set approach)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0217. Contains Duplicate :easy:
|
||||
* DONE 0217. Contains Duplicate :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0217. Contains Duplicate][0217. Contains Duplicate]]
|
||||
:END:
|
||||
@@ -38,10 +38,10 @@ All elements are distinct.
|
||||
|
||||
- ~-10^{9} <= nums[i] <= 10^{9}~
|
||||
|
||||
** TODO Approach
|
||||
** DONE Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
** DONE Python
|
||||
#+begin_src python
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
@@ -53,12 +53,21 @@ class Solution:
|
||||
return False
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
** DONE C++
|
||||
#+begin_src cpp
|
||||
#include <vector>
|
||||
#include <set>
|
||||
class Solution {
|
||||
public:
|
||||
bool containsDuplicate(vector<int>& nums) {
|
||||
|
||||
bool containsDuplicate(std::vector<int>& nums) {
|
||||
std::set<int> s;
|
||||
for (int x: nums) {
|
||||
if (s.contains(x)) {
|
||||
return true;
|
||||
}
|
||||
s.insert(x);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
Reference in New Issue
Block a user