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