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

219. Contains Duplicate II【easy】

时间:2017-10-03 12:00:28      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:position   between   erase   nta   abs   分享   dea   elements   pre   

219. Contains Duplicate II【easy】

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

 

错误解法:

 1 class Solution {
 2 public:
 3     bool containsNearbyDuplicate(vector<int>& nums, int k) {
 4         if (nums.empty()) {
 5             return false;
 6         }
 7         
 8         unordered_map<int, int> my_map;
 9         for (int i = 0; i < nums.size(); ++i) {
10             
11             if (my_map.find(nums[i]) == my_map.end()) {
12                 my_map[nums[i]] = i;
13             }
14             else {
15                 if (abs(i - my_map[nums[i]]) <= k) {
16                     return true;
17                 }
18             }
19         }
20         
21         return false;
22     }
23 };

技术分享

 

解法一:

 1 class Solution {
 2 public:
 3     bool containsNearbyDuplicate(vector<int>& nums, int k) {
 4         if (nums.empty()) {
 5             return false;
 6         }
 7         
 8         unordered_map<int, int> my_map;
 9         for (int i = 0; i < nums.size(); ++i) {
10             if (my_map.find(nums[i]) != my_map.end() && abs(i - my_map[nums[i]]) <= k) {
11                 return true;
12             }
13             my_map[nums[i]] = i;
14         }
15         
16         return false;
17     }
18 };

参考@jianchao.li.fighter 的代码

Well, the basic idea is fairly straightforward. We maintain a mapping mp from a value in nums to its position (index) i. Each time we meet an unseen value, we add it to the map (mp[nums[i]] = i). Otherwise, depending on whether the recorded index mp[nums[i]] and the current index i satisfy i - mp[nums[i]] <= k (node that the new index i is larger than the old index mp[nums[i]]), we return true or update the index (mp[nums[i]] = i). If all the elements have been visited and we have not returned true, we will return false.

 

 解法二:

 1 class Solution {
 2 public:
 3     bool containsNearbyDuplicate(vector<int>& nums, int k)
 4     {
 5        unordered_set<int> s;
 6        
 7        if (k <= 0) return false;
 8        if (k >= nums.size()) k = nums.size() - 1;
 9        
10        for (int i = 0; i < nums.size(); i++)
11        {
12            if (i > k) s.erase(nums[i - k - 1]);
13            if (s.find(nums[i]) != s.end()) return true;
14            s.insert(nums[i]);
15        }
16        
17        return false;
18     }
19 };

参考@luo_seu 的代码,就是维持固定那么多长度的数值

The basic idea is to maintain a set s which contain unique values from nums[i - k] to nums[i - 1],
if nums[i] is in set s then return true else update the set.

 

219. Contains Duplicate II【easy】

标签:position   between   erase   nta   abs   分享   dea   elements   pre   

原文地址:http://www.cnblogs.com/abc-begin/p/7623453.html

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