{"id":3159,"date":"2018-07-14T22:05:57","date_gmt":"2018-07-15T05:05:57","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3159"},"modified":"2018-07-14T22:20:39","modified_gmt":"2018-07-15T05:20:39","slug":"leetcode-287-find-the-duplicate-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-287-find-the-duplicate-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 287. Find the Duplicate Number"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<i>nums<\/i>\u00a0containing\u00a0<i>n<\/i>\u00a0+ 1 integers where each integer is between 1 and\u00a0<i>n<\/i>\u00a0(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> <code>[1,3,4,2,2]\r\n<\/code><b>Output:<\/b> 2<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> [3,1,3,4,2]\r\n<b>Output:<\/b> 3<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>You\u00a0<b>must not<\/b>\u00a0modify the array (assume the array is read only).<\/li>\n<li>You must use only constant,\u00a0<i>O<\/i>(1) extra space.<\/li>\n<li>Your runtime complexity should be less than\u00a0<em>O<\/em>(<em>n<\/em><sup>2<\/sup>).<\/li>\n<li>There is only one duplicate number in the array, but it could be repeated more than once.<\/li>\n<\/ol>\n<h1><strong>Solution1: Binary Search<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>Find the smallest m such that len(nums &lt;= m) &gt; m, which means m is the duplicate number.<\/p>\n<p>In the sorted form [1, 2, &#8230;, m1, m2, m + 1, &#8230;, n]<\/p>\n<p>There are m+1 numbers &lt;= m<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  int findDuplicate(vector&lt;int&gt;&amp; nums) {\r\n    int l = 1;\r\n    int r = nums.size();\r\n    while (l &lt; r) {\r\n      int m = (r - l) \/ 2 + l;\r\n      int count = 0; \/\/ len(nums &lt;= m)\r\n      for (int num : nums)\r\n        if (num &lt;= m) ++count;\r\n      if (count &lt;= m)\r\n        l = m + 1;\r\n      else\r\n        r = m;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n<h1><strong>Solution: Linked list cycle<\/strong><\/h1>\n<p>Convert the problem to find the entry point of the cycle in a linked list.<\/p>\n<p>Take the number in the array as the index of next node.<\/p>\n<p>[1,3,4,2,2]<\/p>\n<p>0-&gt;1-&gt;3-&gt;2-&gt;4-&gt;2 cycle: 2-&gt;4-&gt;2<\/p>\n<p>[3,1,3,4,2]<\/p>\n<p>0-&gt;3-&gt;4-&gt;2-&gt;3-&gt;4-&gt;2 cycle 3-&gt;4-&gt;2-&gt;3<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int findDuplicate(vector&lt;int&gt;&amp; nums) {\r\n    int slow = 0;\r\n    int fast = 0;\r\n    while (true) {\r\n      slow = nums[slow];\r\n      fast = nums[nums[fast]];\r\n      if (slow == fast) break;\r\n    }\r\n    fast = 0;\r\n    while (fast != slow) {\r\n      slow = nums[slow];\r\n      fast = nums[fast];\r\n    }\r\n    return slow;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0nums\u00a0containing\u00a0n\u00a0+ 1 integers where each integer is between 1 and\u00a0n\u00a0(inclusive), prove that at least one duplicate number must exist. Assume that there&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,333,177,334],"class_list":["post-3159","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-duplicate","tag-medium","tag-o1-space","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3159","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=3159"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3159\/revisions"}],"predecessor-version":[{"id":3164,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3159\/revisions\/3164"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}