leetcode 219. Contains Duplicate II

/*
leetcode 219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

题目大意:对于给定的数组,判断nums[i] == nums[j]且i与j的距离不大于k
方法1(TLE):直接遍历数组,超时
方法2(使用map):unordered_map<int, int>map,key记录nums[i],value记录序号i。遍历数组nums,如果找到,由于map[nums[i]]的value是上一次的序号,如果两个序号之差小于等于k那么返回true,否则如果不能找到这个数,则插入(更新)map
方法3(使用set):用一个set维护[i-k-1,i-i]这个范围内的数,这样对于第i个数,如果能在set中找到,那么说明存在,否则继续查找。
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

class Solution {
public:
    //TLE
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        int len = nums.size();
        if (len == 0)
            return false;
        for (int i = 0; i < len; ++i)
        {
            for (int j = 1; j <= k; ++j)
            {
                if (i+j >= len)
                    break;
                if (nums[i] == nums[i + j])
                    return true;
            }
        }

        return false;
    }

    bool containsNearbyDuplicate1(vector<int>& nums, int k)
    {
        unordered_map<int, int>map;
        int len = nums.size();
        for (int i = 0; i < len; ++i)
        {
            if (map.find(nums[i]) != map.end() && i - map[nums[i]] <= k)
                return true;
            else
                map[nums[i]] = i;
        }

        return false;
    }

    bool containsNearbyDuplicate2(vector<int>& nums, int k)
    {
        unordered_set<int> s;

        if (k <= 0)
            return false;
        if (k >= nums.size())
            k = nums.size() - 1;

        for (int i = 0; i < nums.size(); ++i)
        {
            if (i > k)
                s.erase(nums[i-k-1]);
            if (s.find(nums[i]) != s.end())
                return true;
            s.insert(nums[i]);
        }

        return false;
    }
};

void test()
{
    vector<int> v{ 1,2,3,1,4,5,0,3 };
    Solution sol;
    cout << sol.containsNearbyDuplicate1(v, 2) << endl;
    cout << sol.containsNearbyDuplicate1(v, 3) << endl;
    cout << sol.containsNearbyDuplicate2(v, 4) << endl;
    cout << sol.containsNearbyDuplicate2(v, 5) << endl;
}

int main()
{
    test();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值