LeetCode220(2021.4.17)

LC220. 存在重复元素 III

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int> &nums, int k, int t) {
        set<long long> st;
        for (int i = 0; i < nums.size(); i++) {
            auto lb = st.lower_bound((long long) nums[i] - t);
            if (lb != st.end() && *lb <= (long long) nums[i] + t) return true;
            st.insert(nums[i]);
            if (i >= k) st.erase(nums[i - k]);
        }
        return false;
    }
};

发表评论