{"id":4316,"date":"2023-11-08T09:40:32","date_gmt":"2023-11-08T01:40:32","guid":{"rendered":"https:\/\/white-night.club\/?p=4316"},"modified":"2023-11-09T11:07:42","modified_gmt":"2023-11-09T03:07:42","slug":"ad137","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/08\/ad137\/","title":{"rendered":"169.\u00a0\u591a\u6570\u5143\u7d20"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5927\u5c0f\u4e3a\u00a0<code>n<\/code><em>\u00a0<\/em>\u7684\u6570\u7ec4\u00a0<code>nums<\/code>\u00a0\uff0c\u8fd4\u56de\u5176\u4e2d\u7684\u591a\u6570\u5143\u7d20\u3002\u591a\u6570\u5143\u7d20\u662f\u6307\u5728\u6570\u7ec4\u4e2d\u51fa\u73b0\u6b21\u6570\u00a0<strong>\u5927\u4e8e<\/strong>\u00a0<code>\u230a n\/2 \u230b<\/code>\u00a0\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6570\u7ec4\u662f\u975e\u7a7a\u7684\uff0c\u5e76\u4e14\u7ed9\u5b9a\u7684\u6570\u7ec4\u603b\u662f\u5b58\u5728\u591a\u6570\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [3,2,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>3<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [2,2,1,1,1,2,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>2\n<\/pre>\n\n\n\n<p>&nbsp;<strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>n == nums.length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 5 * 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong>\u5c1d\u8bd5\u8bbe\u8ba1\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) \u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u6392\u5e8f<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u6211\u662f\u6765\u5237\u7b97\u6cd5\u7684\uff0c\u4f60\u4eec\u8981\u5e72\u4ec0\u4e48\ud83d\ude31<\/p>\n\n\n\n<p>\u867d\u8bf4\u5982\u6b64\uff0c\u6211\u4e00\u5f00\u59cb\u7528\u7684hashmap\u53bb\u505a\uff0c\u4f46\u53d1\u73b0\u76f4\u63a5\u6392\u4e2a\u5e8f\u8fd4\u56de\u4e0b\u6807n\/2\u5c31\u884c\u4e86&#8230;.\u5982\u6b64\u5982\u6b64\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    int majorityElement(vector&lt;int>&amp; nums) {\r\n        sort(nums.begin(),nums.end());\r\n        int n=nums.size();\r\n        return nums&#91;n\/2];\r\n    }\r\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1asort<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/4316"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=4316"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/4316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=4316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=4316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=4316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}