码迷,mamicode.com
首页 > 其他好文 > 详细

220. Contains Duplicate III

时间:2016-10-17 20:45:35      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

 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

标签:

原文地址:http://www.cnblogs.com/shadowwalker9/p/5971023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!