LeetCode[1346. 检查整数及其两倍数是否存在]
- 哈希表
class Solution {
public:
bool checkIfExist(vector<int>& w) {
unordered_set<int> hash;
for(auto x:w)
{
//对于当前数据,看看哈希表中是否存在满足条件的数
if(hash.count(x * 2) || x % 2 == 0 && hash.count(x / 2))//这里我们要注意x/2必须是整数
return true;
hash.insert(x);
}
return false;
}
};
AI大三在读

浙公网安备 33010602011771号