{"id":5340,"date":"2019-07-22T22:24:40","date_gmt":"2019-07-23T05:24:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5340"},"modified":"2019-07-22T22:25:45","modified_gmt":"2019-07-23T05:25:45","slug":"leetcode-217-contains-duplicate","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-217-contains-duplicate\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 217. Contains Duplicate"},"content":{"rendered":"\n<p>Given an array of integers, find if the array contains any duplicates.<\/p>\n\n\n\n<p>Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.<\/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> [1,2,3,1]\n<strong>Output:<\/strong> true<\/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>[1,2,3,4]\n<strong>Output:<\/strong> false<\/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>[1,1,1,3,3,4,3,2,4,2]\n<strong>Output:<\/strong> true<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: HashTable<\/strong><\/h2>\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, 48ms\/16.5MB\nclass Solution {\npublic:\n  bool containsDuplicate(vector<int>& nums) {\n    unordered_set<int> s;\n    for (int num : nums)\n      if (!s.insert(num).second) return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/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, 28ms\/11.4MB\nclass Solution {\npublic:\n  bool containsDuplicate(vector<int>& nums) {\n    sort(begin(nums), end(nums));\n    for (int i = 1; i < nums.size(); ++i)\n      if (nums[i] == nums[i - 1]) return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,333,222,82,15],"class_list":["post-5340","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-duplicate","tag-easy","tag-hashtable","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5340","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=5340"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5340\/revisions"}],"predecessor-version":[{"id":5343,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5340\/revisions\/5343"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}