-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
217.cpp
44 lines (41 loc) · 1.45 KB
/
217.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__________________________________________________________________________________________________
sample 12 ms submission
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
/*unordered_set<int> encountered;
for (const int n : nums) {
if (encountered.find(n) != encountered.end()) return true;
encountered.emplace(n);
}
return false;*/
if (nums.empty()) return false;
auto result = minmax_element(nums.begin(), nums.end());
size_t range = *result.second - *result.first;
if (range == 0) range = 1;
vector<bool> encountered(range, false);
for (const int n : nums) {
size_t i = n - *result.first;
if (encountered[i] == true) return true;
encountered[i] = true;
}
return false;
}
};
__________________________________________________________________________________________________
sample 11016 kb submission
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
auto size = nums.size();
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[j] == nums[i])
return true;
}
}
return false;
}
};
__________________________________________________________________________________________________