219. Contains Duplicate IIeasy

Posted abc_begin

tags:

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

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 IIeasy的主要内容,如果未能解决你的问题,请参考以下文章

219.Contains Duplicate II

219. Contains Duplicate II

219. Contains Duplicate II

219. Contains Duplicate II

219. Contains Duplicate II

Leetcode 219 Contains Duplicate II STL