{"id":2226,"date":"2016-10-04T13:47:30","date_gmt":"2016-10-04T05:47:30","guid":{"rendered":"http:\/\/boweihe.me\/?p=2226"},"modified":"2016-10-04T13:47:30","modified_gmt":"2016-10-04T05:47:30","slug":"leetcode-220-contains-duplicate-iii","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2226","title":{"rendered":"LeetCode 220. Contains Duplicate III"},"content":{"rendered":"<p>Given an array of integers, find out whether there are two distinct indices <i>i<\/i> and <i>j<\/i> in the array such that the difference between <b>nums[i]<\/b> and\u00a0<b>nums[j]<\/b> is at most <i>t<\/i> and the difference between <i>i<\/i> and <i>j<\/i> is at most <i>k<\/i>.<\/p>\n<div><\/div>\n<h3>\u601d\u8def<\/h3>\n<div>\u57fa\u672c\u601d\u8def\u662f\uff0c\u5047\u8bbe\u5f53\u524d\u8fd0\u884c\u5230nums[i]\u7684\u4f4d\u7f6e\uff0c\u5219\u9700\u8981\u5f80\u524d\u770bk\u4e2a\u6570\uff0c\u5e76\u4e14\u627e\u5230\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u6570x\u6ee1\u8db3 nums[i]-t &lt;= x &lt;=nums[i]+t\u3002\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u5927\u5c0f\u4e3ak\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u89e3\u51b3\u641c\u7d22\u8303\u56f4\u7684\u95ee\u9898\uff08\u95ee\u9898\u5c31\u8f6c\u53d8\u4e3a\uff1a\u627e\u5230\u7b2c\u4e00\u4e2a&gt;=nums[i]-t\u7684\u6570\uff0c\u5224\u65ad\u4ed6\u662f\u5426&lt;=nums[i]+t\uff09\u3002\u6574\u4e2a\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(N * logk)\uff0cN\u4e3a\u6570\u7ec4\u957f\u5ea6\u3002<\/div>\n<div>\u4e0b\u9762\u4ee3\u7801\u4e2d\u7528set\u5b9e\u73b0\uff0cset\u662f\u7528\u7ea2\u9ed1\u6811\u5b9e\u73b0\u7684\u3002<\/div>\n<h3>\u4ee3\u7801<\/h3>\n<div>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool containsNearbyAlmostDuplicate(vector&lt;int&gt;&amp; nums, int k, int t) {\n        if(nums.empty() || k&lt;1 || t&lt;0)\n            return false;\n        k++;\n        set&lt;int&gt; kNumSet;\n        for (int i=0; i&lt;nums.size(); i++){\n            if(i &gt;= k){\n                int oldVal = nums[i-k];\n                kNumSet.erase(oldVal);\n            }\n            auto l_bound = kNumSet.lower_bound(nums[i] - t);\n            if(l_bound != kNumSet.end()){\n                if(*l_bound - t &lt;= nums[i])\n                    return true;\n            }\n            kNumSet.insert(nums[i]);\n        }\n        return false;\n    }\n};<\/pre>\n<p>&nbsp;\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and\u00a0nums[j] is at most t and the difference between i and j is at most k. \u601d\u8def \u57fa\u672c\u601d\u8def\u662f\uff0c\u5047\u8bbe\u5f53\u524d\u8fd0\u884c\u5230nums[i]\u7684\u4f4d\u7f6e\uff0c\u5219\u9700\u8981\u5f80\u524d\u770bk\u4e2a\u6570\uff0c\u5e76\u4e14\u627e\u5230\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u6570x\u6ee1\u8db3 nums[i]-t &lt;= x &lt;=nums[i]+t\u3002\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u5927\u5c0f\u4e3ak\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u89e3\u51b3\u641c\u7d22\u8303\u56f4\u7684\u95ee\u9898\uff08\u95ee\u9898\u5c31\u8f6c\u53d8\u4e3a\uff1a\u627e\u5230\u7b2c\u4e00\u4e2a&gt;=nums[i]-t\u7684\u6570\uff0c\u5224\u65ad\u4ed6\u662f\u5426&lt;=nums[i]+t\uff09\u3002\u6574\u4e2a\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(N * logk)\uff0cN\u4e3a\u6570\u7ec4\u957f\u5ea6\u3002 \u4e0b\u9762\u4ee3\u7801\u4e2d\u7528set\u5b9e\u73b0\uff0cset\u662f\u7528\u7ea2\u9ed1\u6811\u5b9e\u73b0\u7684\u3002 \u4ee3\u7801 class Solution { public: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[27,66,133],"class_list":["post-2226","post","type-post","status-publish","format-standard","hentry","category-study","tag-bst","tag-leetcode-oj","tag-133"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2226"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2226\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}