220. Contains Duplicate III

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了220. Contains Duplicate III相关的知识,希望对你有一定的参考价值。

 1 class Solution {
 2 public:
 3     bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
 4         if (nums.size() < 2) return false;
 5         multimap<int, int> m;
 6         for (int i = 0; i < nums.size(); ++i) {
 7             m.insert(pair<int, int>(nums[i], i));
 8         }
 9         for (auto iter1 = m.begin(); iter1 != m.end(); ++iter1) {
10                 for (auto iter2 = next(iter1); iter2 != m.end() && check(iter2->first, iter1->first, t); ++iter2) {
11                     if (abs(iter2->second - iter1->second) <= abs(k)) return true;
12                 }
13             }
14         return false;
15     }
16 private:
17     bool check(int a, int b, int c) {
18         if (b >= 0) {
19             return (a - b <= c);
20         } else {
21             if (c >= 0) return (a <= b + c);
22             else {
23                 return false;
24             }
25         }
26     }
27 };

 

以上是关于220. Contains Duplicate III的主要内容,如果未能解决你的问题,请参考以下文章

220. Contains Duplicate III

Leetcode 220: Contains Duplicate III

220. Contains Duplicate III

[LeetCode] 220. Contains Duplicate III Java

LeetCode220. Contains Duplicate III

220. Contains Duplicate III(核心:set数组有序/桶排序)