{"id":9056,"date":"2021-12-05T20:13:40","date_gmt":"2021-12-06T04:13:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9056"},"modified":"2021-12-05T20:14:40","modified_gmt":"2021-12-06T04:14:40","slug":"leetcode-219-contains-duplicate-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-219-contains-duplicate-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 219. Contains Duplicate II"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>, return&nbsp;<code>true<\/code>&nbsp;if there are two&nbsp;<strong>distinct indices<\/strong>&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>j<\/code>&nbsp;in the array such that&nbsp;<code>nums[i] == nums[j]<\/code>&nbsp;and&nbsp;<code>abs(i - j) &lt;= k<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,1], k = 3\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,0,1,1], k = 1\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,1,2,3], k = 2\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>0 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Sliding Window + Hashtable<\/strong><\/p>\n\n\n\n<p>Hashtable to store the last index of a number.<\/p>\n\n\n\n<p>Remove the number if it&#8217;s k steps behind the current position.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool containsNearbyDuplicate(vector<int>& nums, int k) {\n    unordered_map<int, int> m; \/\/ num -> last index\n    for (int i = 0; i < nums.size(); ++i) {\n      if (i > k && m[nums[i - k - 1]] < i - k + 1)\n        m.erase(nums[i - k - 1]);\n      if (m.count(nums[i])) return true;\n      m[nums[i]] = i;\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k, return&nbsp;true&nbsp;if there are two&nbsp;distinct indices&nbsp;i&nbsp;and&nbsp;j&nbsp;in the array such that&nbsp;nums[i] == nums[j]&nbsp;and&nbsp;abs(i &#8211; j) &lt;= k. Example 1: Input: nums&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[82,177,215],"class_list":["post-9056","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-hashtable","tag-medium","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9056"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9056\/revisions"}],"predecessor-version":[{"id":9058,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9056\/revisions\/9058"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}