{"id":1920,"date":"2016-03-30T11:32:43","date_gmt":"2016-03-30T03:32:43","guid":{"rendered":"http:\/\/boweihe.me\/?p=1920"},"modified":"2016-03-30T11:32:43","modified_gmt":"2016-03-30T03:32:43","slug":"leetcode-287-find-the-duplicate-number-%e6%99%ba%e5%95%86%e8%a2%ab%e7%a2%be%e5%8e%8b%ef%bc%81","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1920","title":{"rendered":"LeetCode 287. Find the Duplicate Number &#124; \u667a\u5546\u88ab\u78be\u538b\uff01"},"content":{"rendered":"<p><strong>\u770b\u4e86LeetCode\u4e0a\u725b\u4eba\u7684\u89e3\u7b54\uff0c\u611f\u89c9\u6211\u7684\u667a\u5546\u88ab\u78be\u538b\u4e86~<\/strong><\/p>\n<h3>\u539f\u9898<\/h3>\n<p>Given an array <i>nums<\/i> containing <i>n<\/i> + 1 integers where each integer is between 1 and <i>n<\/i> (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.<br \/>\n\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b<em>n+1<\/em>\u4e2a\u6574\u6570\u7684\u6570\u7ec4<em>nums<\/em>\uff0c\u8be5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u90fd\u5728[1,n]\u7684\u8303\u56f4\u5185\uff0c\u5047\u8bbe\u6570\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a\u6570\u662f\u91cd\u590d\u7684\uff0c\u627e\u5230\u90a3\u4e2a\u91cd\u590d\u7684\u6570\u3002<br \/>\n<b>Note|\u63d0\u793a:<\/b><\/p>\n<ol>\n<li>You <b>must not<\/b> modify the array (assume the array is read only).| \u00a0 \u00a0<strong>\u4e0d\u80fd\u6539\u52a8\u6570\u7ec4\u5185\u5bb9<\/strong><\/li>\n<li>You must use only constant, <i>O<\/i>(1) extra space.| \u00a0 \u00a0<strong>\u5728O(1)\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210<\/strong><\/li>\n<li>Your runtime complexity should be less than <code>O(n<sup>2<\/sup>)<\/code>.| \u00a0 \u00a0<strong>\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u8d85\u8fc7O(n^2)<\/strong><\/li>\n<li>There is only one duplicate number in the array, but it could be repeated more than once.| \u00a0 \u00a0<strong>\u53ea\u6709\u4e00\u4e2a\u91cd\u590d\u6570\u5b57\uff0c\u4f46\u662f\u53ef\u80fd\u91cd\u590d\u8d85\u8fc7\u4e00\u6b21<\/strong><\/li>\n<\/ol>\n<h3>\u601d\u8def1\uff1aO(nlogn)\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u82f1\u6587\u7248\u89e3\u91ca\uff1a<a href=\"https:\/\/leetcode.com\/discuss\/60830\/solutions-explanation-space-without-changing-input-array\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/leetcode.com\/discuss\/60830\/solutions-explanation-space-without-changing-input-array<\/a><br \/>\n\u8fd9\u4e2a\u89e3\u6cd5\u6d89\u53ca\u5230\u9e3d\u5de2\u539f\u7406\u3002<br \/>\n\u5047\u8bbe\u6709<em>n<\/em>=10\u7684\u4e00\u4e2a\u6570\u7ec4\uff08\u5927\u5c0f\u662f<em>n+1<\/em>\uff09\uff0c\u90a3\u6211\u76ee\u524d\u7684\u641c\u7d22\u8303\u56f4\u662f[1,10]\uff0c\u5148\u627e\u4e2d\u95f4\u7684\u6570<em>mid<\/em>=5.\u73b0\u5728\u6211\u4eec\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u5e76\u7edf\u8ba1&lt;=5\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8bb0\u4f5c<em>n_lteq5<\/em>\u597d\u4e86\uff0c\u90a3\u4e48\u6709\uff1a<\/p>\n<ul>\n<li>\u5982\u679c<em>n_lteq5<\/em>&gt;5\uff0c\u90a3\u5c31\u67096\u4e2a\u6570\u5b57\u5360\u4e86\u672c\u6765\u53ea\u67095\u4e2a\u7684\u5751\u4f4d\uff0c\u90a3\u76ee\u6807\u6570\u5b57\u80af\u5b9a\u662f\u5728[1,5]\u7684\u8303\u56f4\u5185\u4e86\uff1b<\/li>\n<li>\u5982\u679cn_lteq5&lt;=5\uff0c\u90a3\u524d\u9762\u7684\u5143\u7d20\u90fd\u633a\u5b88\u89c4\u77e9\u7684\uff0c\u5f97\u770b\u770b[6,10]\u91cc\u5934\u54ea\u4e2a\u6570\u5b57\u5728\u4f5c\u602a\uff1b<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u6bcf\u4e00\u6b21\u5224\u65ad\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u90fd\u4f1a\u7f29\u5c0f\u4e00\u534a\uff0c\u4e00\u5171n+1\u4e2a\u6570\u5b57\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(nlogn)<br \/>\n\u611f\u89c9\u78b0\u5230\u4e86\u5f88\u591a\u4e8c\u5206\u601d\u60f3\u7684\u9898\u76ee\u554a\uff0c\u5173\u952e\u70b9\u5c31\u662f\u8981\u627e\u5230\u4e00\u4e2a\u4ec0\u4e48\u5212\u5206\u65b9\u5f0f\u628a\u95ee\u9898\u7684\u89c4\u6a21\u7f29\u5c0f\u6389\uff0c\u800c\u5728\u8fd9\u9053\u9898\u91cc\u5934\u7528\u5230\u7684\u5c31\u662f\u9e3d\u5de2\u539f\u7406\u3002<\/p>\n<h3>\u601d\u8def1\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int countLtEq(const vector&lt;int&gt;&amp; nums, int n){\n        int count = 0;\n        for(int i=0; i&lt;nums.size(); i++){\n            if(nums[i] &lt;= n)\n                count ++;\n        }\n        return count;\n    }\n    int findDuplicate(vector&lt;int&gt;&amp; nums) {\n        int n = nums.size() - 1;\n        int min = 1, max = n;\n        int mid;\n        int ltEqCnt;\n        while(min &lt; max){\n            mid = (min + max)\/2;\n            ltEqCnt = countLtEq(nums, mid);\n            if(ltEqCnt &lt;= mid){\n                \/\/[min, mid]\u662f\u6b63\u5e38\u7684\uff0c\u627e\u53e6\u4e00\u8fb9\n                min = mid+1;\n            } else {\n                max = mid;\n            }\n        }\n        return min; \/\/ min == max\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n<h3>\u63d2\u66f2\uff1a\u5982\u679c\u53ef\u4ee5\u6539\u53d8\u6570\u7ec4\u5143\u7d20\u5462\uff1f<\/h3>\n<p>\u611f\u89c9\u5728\u5251\u6307Offer\u4e0a\u9047\u5230\u8fc7\u7c7b\u4f3c\u7684\u9898\u76ee\u3002\u5982\u679c\u53ef\u4ee5\u6539\u53d8\u6570\u7ec4\u5143\u7d20\u7684\u8bdd\uff0c\u53ef\u4ee5\u6309\u7167\u4e0b\u9762\u7684\u601d\u8def\u8fdb\u884c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(<em>n<\/em>)\u3002\u57fa\u672c\u601d\u60f3\u662f\uff0c\u4e00\u4e2a\u841d\u535c\u4e00\u4e2a\u5751\uff0c\u628a\u641c\u7d22\u5230\u7684\u6570\u653e\u5230\u81ea\u5df1\u5e94\u8be5\u5728\u7684\u4f4d\u7f6e\u4e0a\uff0c\u6bd4\u59821\u5e94\u8be5\u653e\u5728\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u4e0a\uff0c\u5982\u679c\u540e\u9762\u518d\u627e\u5230\u4e00\u4e2a1\uff0c\u6211\u4eec\u5c31\u4f1a\u53d1\u73b01\u8fd9\u4e2a\u4f4d\u7f6e\u7684\u5751\u88ab\u524d\u4e00\u4e2a1\u5360\u4e86\uff0c\u90a3\u5c31\u51b2\u7a81\u4e86\u3002<br \/>\n\u65b9\u4fbf\u8d77\u89c1\uff0c\u6570\u7ec4\u7684\u4f4d\u7f6e\u63091,2,3,&#8230;\u63cf\u8ff0\uff08\u800c\u975e0,1,2&#8230;\uff09\u3002\u5047\u8bbe\u6709\u6570\u7ec4<span class=\"lang:default decode:true crayon-inline\">[5,4,2,3,1,5,6]<\/span>\u00a0\uff0c\u5f53\u524d\u5de5\u4f5c\u7684\u4f4d\u7f6e<em>pos<\/em>=1<\/p>\n<ul>\n<li>\u5148\u770b\u7b2c<em>pos<\/em>=1\u4e2a\u6570\u5b57<em>5<\/em>\uff0c\u5728\u8bbe\u60f3\u7684\u6392\u597d\u5e8f\u7684\u5e8f\u5217\u91cc\uff0c<em>5<\/em>\u5e94\u8be5\u5c31\u5728\u7b2c5\u4e2a\u4f4d\u7f6e\u4e0a\uff0c\u56e0\u6b64\u6211\u4eec\u628a<em>5<\/em>\u548c\u7b2c5\u4e2a\u4f4d\u7f6e\u4e0a\u73b0\u6709\u7684\u6570\u5b57<em>1<\/em>\u8fdb\u884c\u8c03\u6362\uff0c\u7ed3\u679c\u5c31\u53d8\u6210\u4e86<span class=\"lang:default decode:true crayon-inline\">[1,4,2,3,5,5,6]<\/span>\u00a0\uff1b<\/li>\n<li>\u770b\u6362\u56de\u6765\u7684\u6570\u5b57<em>1<\/em>\uff0c\u7531\u4e8e\u5df2\u7ecf\u57281\u53f7\u4f4d\u4e0a\uff0c\u8fd9\u6b21\u5904\u7406\u5c31\u7ed3\u675f\u4e86\uff0c\u5f80\u524d\u632a\u4e00\u4e0b\uff0c<em>pos<\/em>=2\uff0c\u770b\u4e0b\u4e00\u4e2a\u6570\u5b57\uff1b<\/li>\n<li><em>pos<\/em>=2\u7684\u6570\u5b57\u662f<em>4<\/em>\uff0c\u672c\u6765\u5e94\u8be5\u57284\u53f7\u4f4d\u4e0a\uff0c\u56e0\u6b64\u628a<em>4<\/em>\u548c4\u53f7\u4f4d\u4e0a\u7684 <em>3<\/em>\u8c03\u6362\uff0c\u6570\u7ec4\u53d8\u6210<span class=\"lang:default decode:true crayon-inline\">[1,3,2,4,5,5,6]<\/span>\u00a0\uff1b<\/li>\n<li>\u8fd9\u65f6\u5019\u4e8b\u60c5\u6ca1\u5b8c\uff0c\u56e0\u4e3a\u6362\u56de\u6765\u7684<em>3<\/em>\u4e0d\u5728\u5e94\u8be5\u6709\u76843\u53f7\u4f4d\u4e0a\uff0c\u56e0\u6b64\u628a<em>3<\/em>\u548c3\u53f7\u4f4d\u4e0a\u5360\u7740\u7684<em>2<\/em>\u8c03\u6362\uff0c\u6570\u7ec4\u53d8\u6210<span class=\"lang:default decode:true crayon-inline\">[1,2,3,4,5,5,6]<\/span>\u00a0\uff1b<\/li>\n<li>\u6362\u56de\u7684\u6570\u5b572\u662f\u6b63\u786e\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64<em>pos<\/em>++\uff0c\u53d8\u6210<em>pos<\/em>=3\uff0c\u7c7b\u4f3c\u5730\u68c0\u67e5<em>pos<\/em>=4, 5\u53d1\u73b0\u90fd\u7b26\u5408\u8981\u6c42\uff1b<\/li>\n<li>\u5f53<em>pos<\/em>=6\u65f6\uff0c\u6b64\u65f6\u4f4d\u7f6e\u4e0a\u7684\u5143\u7d20\u662f<em><span style=\"color: #008000;\">5<\/span><\/em>\uff0c\u4e8e\u662f\u6211\u4eec\u60f3\u628a\u8fd9\u4e2a<span style=\"color: #008000;\"><em>5<\/em><\/span>\u5f52\u4e3a\u52305\u53f7\u4f4d\uff0c\u4f46\u662f\u68c0\u67e55\u53f7\u4f4d\u7684\u65f6\u5019\u6211\u4eec\u53d1\u73b0\uff0c\u4e0a\u9762\u5df2\u7ecf\u6709\u4e00\u4e2a<span style=\"color: #ff6600;\"><em>5<\/em><\/span>\u4e86\uff0c\u8fd9\u5c31\u8bf4\u660e<em>5<\/em>\u662f\u91cd\u590d\u6570\u503c\u4e86\uff0c\u641e\u5b9a<\/li>\n<\/ul>\n<p>\u53ef\u80fd\u6709\u95ee\uff0c\u90a3\u5982\u679c\u539f\u6765\u7684\u6570\u7ec4\u662f<span class=\"lang:default decode:true crayon-inline \">[5,4,2,3,1,6,5]<\/span>\u00a0\u5462\uff1f\u5f88\u663e\u7136\uff0c\u5982\u679c\u524d\u9762\u627e\u5230<em>pos<\/em>=6\u7684\u65f6\u5019\u90fd\u6ca1\u53d1\u751f\u51b2\u7a81\uff0c\u90a3\u7f6a\u9b41\u7978\u9996\u80af\u5b9a\u5728\u6700\u540e\u4e2a\u5143\u7d20\u4e0a\u9762\u4e86\u3002<br \/>\n\u8fd9\u79cd\u65b9\u6cd5\u53ea\u8981\u904d\u5386\u4e00\u6b21\u6570\u7ec4\u5e76\u505a\u76f8\u5e94\u66ff\u6362\u5c31\u884c\uff0c\u9664\u4e86\u4f1a\u6539\u53d8\u6570\u7ec4\u5185\u5bb9\u5916\uff0c\u5176\u4ed6\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u53ef\u60dc\u554a\u3002<\/p>\n<h3>\u601d\u8def2\uff1aO(n)\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<p>\u5927\u795e\uff0c\u5927\u5bb6\u5feb\u53bb\u819c\u62dc\uff1a<a href=\"http:\/\/keithschwarz.com\/interesting\/code\/?dir=find-duplicate\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/keithschwarz.com\/interesting\/code\/?dir=find-duplicate<\/a><br \/>\n<em>\u636e\u8bf4\u8d8a\u662f\u7ecf\u5178\u7684\u4ee3\u7801\u884c\u6570\u8d8a\u662f\u5199\u7684\u7b80\u5355\uff0c\u7136\u800c\u4f60\u5e76\u4e0d\u80fd\u770b\u61c2&#8230;<\/em><br \/>\n\u4f3c\u4e4e\u662f\u628a\u6570\u7ec4\u4e2d\u7684\u503c\u770b\u505a\u5355\u94fe\u8868\u7684next\u6307\u9488\u7684\u503c\u7684\uff0c\u8fd8\u5728\u7406\u89e3\u4e2d~\u8fd9\u4e2a\u7b97\u6cd5\u7406\u8bba\u4e0a\u662f\u6b63\u786e\u7684\u3002<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u770b\u4e86LeetCode\u4e0a\u725b\u4eba\u7684\u89e3\u7b54\uff0c\u611f\u89c9\u6211\u7684\u667a\u5546\u88ab\u78be\u538b\u4e86~ \u539f\u9898 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. \u7ed9\u5b9a\u4e00\u4e2a\u5305\u542bn+1\u4e2a\u6574\u6570\u7684\u6570\u7ec4nums\uff0c\u8be5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u90fd\u5728[1,n]\u7684\u8303\u56f4\u5185\uff0c\u5047\u8bbe\u6570\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a\u6570\u662f\u91cd\u590d\u7684\uff0c\u627e\u5230\u90a3\u4e2a\u91cd\u590d\u7684\u6570\u3002 Note|\u63d0\u793a: You must not modify the array (assume the array is read only).| [&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":[66,185],"class_list":["post-1920","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-185"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1920","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=1920"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1920\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}