leecode_220 Contains Duplicate III

原创 2016年06月02日 11:31:12

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.

两月没刷没手感了, nk的复杂度解法超时,

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        int len=nums.size();
        if (len<=1 || k==0 )
            return false;
            
        multiset<int> tree;
        
        for (int i=0;i<nums.size();i++){
            if (tree.size()>k)
                  tree.erase(nums[i-k-1]);
            if (tree.size()>0){
                 multiset<int>::iterator low_bound=tree.lower_bound(nums[i]-abs(t));//返回>=nums[i]-abs(t)的第一个值;
                 multiset<int>::iterator up_bound=tree.lower_bound(nums[i]);//返回>nums[i]的第一个值;
                 
                 if (low_bound!=tree.end() && abs(*low_bound-nums[i])<=t)
                     return true;
                 if (up_bound!=tree.end() && abs(*up_bound-nums[i])<=t)
                    return true;
            }
            tree.insert(nums[i]);
        }
        
        
        return false;
        
        
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode220-Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array suc...

Contains Duplicate III - LeetCode 220

题目描述: Given an array of integers, find out whether there are two distinct indices i and j in the ar...

[leetcode ]220.Contains Duplicate III

题目: Given an array of integers, find out whether there are two distinct indices i and j in the arra...

【LEETCODE】220-Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array  ...

Leetcode 220. Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array suc...

220. Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array s...

217, 219,220. Contains Duplicate I, II, III

Given an array of integers, find if the array contains any duplicates. Your function should return ...

[Leetcode 220, Medium] Contains Duplicates III

[Leetcode 220, Medium] Contains Duplicates III

[LeetCode]217. Contains Duplicate&219. Contains Duplicate II&220. Contains Duplicate III

217. Contains Duplicate 219. Contains Duplicate II 220. Contains Duplicate III

[Leetcode]Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array su...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)