LeetCode 217 Contains Duplicate

Tag: LeetCode Posted on 2022-03-05 16:56:02 Edited on 2022-03-05 16:56:04 Views: 121

概述

https://leetcode.com/problems/contains-duplicate/

暴力法

直接遍历 + set。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> s;
        for (auto n : nums) {
            if (s.count(n)) return true;
            s.insert(n);
        }
        return false;
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/