-
Notifications
You must be signed in to change notification settings - Fork 0
/
26_CheckIfN.cpp
62 lines (56 loc) · 1.22 KB
/
26_CheckIfN.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://leetcode.com/problems/check-if-n-and-its-double-exist/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool checkIfExist(vector<int> &arr)
{
unordered_set<int> st;
for(auto &it: arr){
if (it == 0){
if (st.count(it)) return true;
}
st.insert(it);
}
for(auto &it: arr){
if (it && st.count(it*2)) return true;
}
return false;
}
};
class Solution
{
// WA: 7 1 14 11 0
public:
bool checkIfExist(vector<int> &arr)
{
unordered_map<int, int> mp;
for (auto &it : arr)
{
mp[it]++;
}
for (auto &it : mp)
{
cout << it.first << " " << it.second << "\n";
int check = it.first * 2;
cout << check << "\n";
if (check == 0 && mp[check] == 1){
continue;
}
if (mp[check]){
return true;
}
}
return false;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> nums = {-2,0,10,-19,4,6,-8};
Solution Obj1;
cout << Obj1.checkIfExist(nums);
return 0;
}