{"id":6068,"date":"2024-04-27T14:07:30","date_gmt":"2024-04-27T06:07:30","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=6068"},"modified":"2024-04-27T14:07:30","modified_gmt":"2024-04-27T06:07:30","slug":"189-%e8%bd%ae%e8%bd%ac%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/27\/189-%e8%bd%ae%e8%bd%ac%e6%95%b0%e7%bb%84\/","title":{"rendered":"189.\u8f6e\u8f6c\u6570\u7ec4"},"content":{"rendered":"<h2>199\u3001\u591a\u6570\u5143\u7d20\u3010\u6570\u7ec4\u3011<\/h2>\n<h3>1\u3001<a href=\"https:\/\/leetcode.cn\/problems\/majority-element\/\">\u591a\u6570\u5143\u7d20<\/a><\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5927\u5c0f\u4e3a <code>n<\/code> \u7684\u6570\u7ec4 <code>nums<\/code> \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 <strong>\u5927\u4e8e<\/strong> <code>\u230a n\/2 \u230b<\/code> \u7684\u5143\u7d20\u3002<\/p>\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<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><code>\u8f93\u5165\uff1anums = [3,2,3]\n\u8f93\u51fa\uff1a3<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><code>\u8f93\u5165\uff1anums = [2,2,1,1,1,2,2]\n\u8f93\u51fa\uff1a2<\/code><\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>n == nums.length<\/code><\/li>\n<li><code>1 &lt;= n &lt;= 5 * 104<\/code><\/li>\n<li><code>-109 &lt;= nums[i] &lt;= 109<\/code><\/li>\n<\/ul>\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<h3>2\u3001\u89e3\u9898\u601d\u8def<\/h3>\n<h4>1\u3001\u6469\u5c14\u6295\u7968<\/h4>\n<p>1\u3001\u4e24\u4e2a\u91cd\u8981\u63a8\u8bba<\/p>\n<p>2\u3001\u63a8\u8bba\u4e00\uff1a \u82e5\u8bb0 \u4f17\u6570 \u7684\u7968\u6570\u4e3a +1+1+1 \uff0c\u975e\u4f17\u6570 \u7684\u7968\u6570\u4e3a \u22121-1\u22121 \uff0c\u5219\u4e00\u5b9a\u6709\u6240\u6709\u6570\u5b57\u7684 \u7968\u6570\u548c &gt;0&gt; 0&gt;0 \u3002<\/p>\n<p>3\u3001\u63a8\u8bba\u4e8c\uff1a \u82e5\u6570\u7ec4\u7684\u524d aaa \u4e2a\u6570\u5b57\u7684 \u7968\u6570\u548c =0= 0=0 \uff0c\u5219 \u6570\u7ec4\u5269\u4f59 (n\u2212a)(n-a)(n\u2212a) \u4e2a\u6570\u5b57\u7684 \u7968\u6570\u548c\u4e00\u5b9a\u4ecd &gt;0&gt;0&gt;0 \uff0c\u5373\u540e (n\u2212a)(n-a)(n\u2212a) \u4e2a\u6570\u5b57\u7684 \u4f17\u6570\u4ecd\u4e3a xxx \u3002<\/p>\n<p>4\u3001\u5982\u56fe<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240420235836345-300x228.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240420235836345-300x228.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div>.png)<\/p>\n<h5>1\u3001\u8865\u5145<\/h5>\n<p>1\u3001\u5728\u672c\u6587\u4e2d\uff0c\u201c\u6570\u7ec4\u4e2d\u51fa\u73b0\u6b21\u6570\u8d85\u8fc7\u4e00\u534a\u7684\u6570\u5b57\u201d \u88ab\u79f0\u4e3a <strong>\u201c\u4f17\u6570\u201d<\/strong> \u3002<\/p>\n<p>2\u3001\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6570\u5b66\u4e2d\u4f17\u6570\u7684\u5b9a\u4e49\u4e3a \u201c\u6570\u7ec4\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u6570\u5b57\u201d \uff0c\u4e0e\u672c\u6587\u5b9a\u4e49\u4e0d\u540c\u3002<\/p>\n<h5>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h5>\n<pre><code class=\"language-java\">class Solution {\n    public int majorityElement(int[] nums) {\n        int x = 0; \/\/\u8fd4\u56de\u6700\u540e\u7ed3\u679c\n        int temp = 0; \/\/\u4e34\u65f6\u53d8\u91cf\uff0c\u7528\u6765\u8bb0\u5f55\u8fd9\u4e2a\u6570\u662f\u5426&gt;0\n        for(int num :nums){\n            if(temp==0){ \/\/\u5982\u679c==0,\u6211\u4eec\u5c31\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6570\n                x = num; \/\/\u628a\u4e0b\u4e00\u4e2a\u6570\u8d4b\u503c\u7ed9x\n            }\n            temp+= num ==x ?1:-1; \/\/\u4e34\u65f6\u53d8\u91cf\u8fdb\u884c\u5224\u65ad\uff0c\u662f\u5426+1\uff0c\u8fd8\u662f-1\n        }\n        return x;\n    }\n}<\/code><\/pre>\n<h4>3\u3001\u6570\u7ec4\u6392\u5e8f\u6cd5<\/h4>\n<h5>1\u3001\u89e3\u9898\u601d\u8def<\/h5>\n<ul>\n<li>\u5c06num\u6570\u7ec4\u6392\u5e8f\uff0c<strong>\u6570\u7ec4\u4e2d\u70b9\u7684\u5143\u7d20<\/strong>\uff0c\u4e00\u5b9a\u662f\u4f17\u6570<\/li>\n<\/ul>\n<h5>\u6ce8\u610f\u8fd9\u91cc\u6709\u4e2a\u5148\u51b3\u6761\u4ef6<\/h5>\n<p>1\u3001\u6839\u636e\u9898\u76ee\u8981\u6c42\u6765\u8bf4<\/p>\n<p>2\u3001\u6211\u4eec\u8fd9\u91cc\u7684\u4f17\u6570\uff08\u7ed3\u679c\uff09\u7684\u4e2a\u6570\u4e00\u5b9a\u8981&gt;nums.length\/2<\/p>\n<h5>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h5>\n<pre><code class=\"language-java\">class Solution {\n    public int majorityElement(int[] nums) {\n        \/\/ \u5148\u6392\u5e8f\n        Arrays.sort(nums);\n        \/\/ \u56e0\u4e3a\u591a\u5143\u4e2a\u6570\u5fc5\u987b\u5927\u4e8enums.length\/2\u3002\u6240\u6709\u6211\u4eec\u5c31\u53ef\u4ee5\u6839\u636e\u6c42\u6570\u7ec4\u4e2d\u4f4d\u6570\u6765\u83b7\u53d6\u7ed3\u679c\u7b54\u6848\n        return nums[nums.length\/2];\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>199\u3001\u591a\u6570\u5143\u7d20\u3010\u6570\u7ec4\u3011 1\u3001\u591a\u6570\u5143\u7d20 \u7ed9\u5b9a\u4e00\u4e2a\u5927\u5c0f\u4e3a n \u7684\u6570\u7ec4 nums \uff0c\u8fd4\u56de\u5176\u4e2d\u7684\u591a\u6570\u5143\u7d20\u3002\u591a\u6570\u5143\u7d20\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[67,68,69],"class_list":["post-6068","post","type-post","status-publish","format-standard","hentry","category-15","tag-java","tag-leet-code","tag-69"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6068"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6068\/revisions"}],"predecessor-version":[{"id":6070,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6068\/revisions\/6070"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6068"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}