美文网首页Leetcode
Leetcode 219. Contains Duplicate

Leetcode 219. Contains Duplicate

作者: SnailTyan | 来源:发表于2018-10-24 18:14 被阅读1次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Contains Duplicate II

    2. Solution

    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int, int> m;
            for(int i = 0; i < nums.size(); i++) {
                if(m.find(nums[i]) != m.end() && (i - m[nums[i]] <= k)) {
                    return true;
                }
                m[nums[i]] = i;
            }
            return false;
        }
    };
    

    Reference

    1. https://leetcode.com/problems/contains-duplicate-ii/description/

    相关文章

      网友评论

        本文标题:Leetcode 219. Contains Duplicate

        本文链接:https://www.haomeiwen.com/subject/sybxtqtx.html