LeetCode[1346. 检查整数及其两倍数是否存在]

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;
    }
};
posted @ 2022-09-25 19:43  Sheldon2  阅读(18)  评论(0)    收藏  举报