首页 > 代码库 > 220. Contains Duplicate III

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