题目描述:给你一个整数数组 arr,请你检查是否存在两个整数 N 和 M,满足 N 是 M 的两倍(即,N = 2 * M)。![在这里插入图片描述](https://img-blog.csdnimg.cn/20200729211559122.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xfY19jX2M=,size_16,color_FFFFFF,t_70)
解题思路:注意0这个特殊值。
代码:
class Solution {
public:
bool checkIfExist(vector<int>& arr) {
unordered_map<int,int>s;
for(int x:arr)
s[x]++;
for(int x:arr)
if(x!=0&&s.find(x*2)!=s.end())
return true;
else if(x==0&&s[x]>=2)
return true;
return false;
}
};