LeetCode|Contains Duplicate*

Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

思路:
重复次数不限制,所以只能用hash表来判断了

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> um;
        for(int i = 0; i < nums.size(); i++){
            unordered_map<int, int> :: iterator it = um.find(nums[i]);
            if(it == um.end()) um[nums[i]] = 1;
            else return true;
         } return false;
    }
};

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 absolute difference between i and j is at most k.

方法一:
hash表,key->val。val用来存key出现的下标,如果第二次找到该key并且下标距离大于k就更新val为

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

Contains Duplicate III

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

方法一:
哈希法,根据元素值最大距离为t来制作hash函数。同时维持一个大小为k+1的窗口。
空间复杂度O(k),时间复杂度O(n)

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        int n = nums.size();
        if(n == 0 || k < 1 || t < 0) return false;
        this->width = t+1;
        unordered_map<long long, long long> um;
        unordered_map<long long, long long> :: iterator it;
        for(int i = 0; i < n; i++){
            long long hashId = getHashId(nums[i]);
            if( um.find(hashId) != um.end()
            || ( (it = um.find(hashId-1)) != um.end() && nums[i] - it->second <= t)
            || ( (it = um.find(hashId+1)) != um.end() && it->second - nums[i] <= t) )
                return true;
            um[hashId] = nums[i];
            if(i >= k) um.erase(getHashId(nums[i-k])); 
        } return false;
    }
    private:
    long long getHashId(long long x) const{
        if(x < 0) return (x+1)/width - 1;
        return x/width;
    }
    long long width;
};

方法二:
维持一个大小为k+1的窗口,空间复杂度为O(k)。时间复杂度O( n *log k )

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        int n = nums.size();
        if(n == 0 || k < 1 || t < 0) return false;
        set<long long> s;
        for(int i = 0; i < n; i++){
            auto it = s.lower_bound(nums[i] - t);
            if(it != s.end() && abs(nums[i] - *it) <= t) return true;
            s.insert(nums[i]);
            if(i >= k) s.erase(nums[i-k]);
        } return false;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值