Leetcode 217

217. Contains Duplicate

挺简单的一道题目,一开始想用暴力点求解,但是感觉太蠢了,用hash表秒过

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> hashmap;
        for (int i = 0; i < nums.size(); i++) {
            unordered_map<int, int>::const_iterator got = hashmap.find(nums[i]);
            if (got == hashmap.end()) {
                hashmap.insert({nums[i], i});
                continue;
            } else {
                return true;
            }
        }
        return false;
    }
};

Lofi&Cafe

Blog about tech and life!


2022-07-10