{"id":417,"date":"2020-09-26T10:39:09","date_gmt":"2020-09-26T02:39:09","guid":{"rendered":"http:\/\/39.96.58.60\/?p=417"},"modified":"2022-10-18T16:39:23","modified_gmt":"2022-10-18T08:39:23","slug":"leetcode-%e9%9d%a2%e8%af%95%e9%a2%98-17-10-%e4%b8%bb%e8%a6%81%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=417","title":{"rendered":"Leetcode \u9762\u8bd5\u9898 17.10. \u4e3b\u8981\u5143\u7d20"},"content":{"rendered":"<p>\u6570\u7ec4\u4e2d\u5360\u6bd4\u8d85\u8fc7\u4e00\u534a\u7684\u5143\u7d20\u79f0\u4e4b\u4e3a\u4e3b\u8981\u5143\u7d20\u3002\u7ed9\u5b9a\u4e00\u4e2a<strong>\u6574\u6570<\/strong>\u6570\u7ec4\uff0c\u627e\u5230\u5b83\u7684\u4e3b\u8981\u5143\u7d20\u3002\u82e5\u6ca1\u6709\uff0c\u8fd4\u56de-1\u3002<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[1,2,5,9,5,9,5,5,5]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>5<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[3,2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>-1<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[2,2,1,1,1,2,2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>2<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<p>\u4f60\u6709\u529e\u6cd5\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1) \u5185\u5b8c\u6210\u5417\uff1f<\/p>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u4f4d\u8fd0\u7b97\u3001 \u6570\u7ec4\u3001 \u5206\u6cbb\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8683.86% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8662.52%\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5bf9\u62fc\u6d88\u8017\uff0c\u6469\u5c14\u6295\u7968\n    \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\n    \u4e0d\u540c\u6570\u5b57\u4e00\u4e00\u5bf9\u62fc\u6d88\u8017\uff0c\u5982\u679c\u6709\u5269\u4f59\uff0c\u5219\u5b58\u5728\u4e3b\u8981\u5143\u7d20(\u4eba\u6570\u6700\u591a\uff0c\u4e14\uff0c\u4eba\u6570\u5728\u4e00\u534a\u4ee5\u4e0a)\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def majorityElement(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        major = nums[0] # \u8bb0\u5f55\u5f53\u524d\u5143\u7d20\n        count = 0   # \u5f53\u524d\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u7edf\u8ba1\n        for i in range(n):\n            if nums[i] == major:    # \u5982\u679c\u4e0e\u5f53\u524d\u5143\u7d20\u76f8\u540c\uff0c\u5219\u6b21\u6570+1\n                count += 1\n            else:   # \u4e0d\u540c\uff0c\u5219\u5bf9\u62fc\u6d88\u8017\uff0c\u6b21\u6570\u51cf\u4e00\n                count -= 1\n            if count == 0 and i &lt; n-1:  # \u5982\u679c\u5f53\u524d\u5143\u7d20\u88ab\u6d88\u8017\u6b86\u5c3d\uff0c\u5f53\u524d\u5143\u7d20\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\n                major = nums[i+1]\n\n        if count &gt; 0:   # \u5982\u679c\u5f53\u524d\u5143\u7d20\u7684\u7edf\u8ba1\u6b21\u6570\u5927\u4e8e0\uff0c\u5219\u5b58\u5728\u4e3b\u8981\u5143\u7d20\n            return major\n        else:       # \u5426\u5219\uff0c\u4e0d\u5b58\u5728\n            return -1<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4\u4e2d\u5360\u6bd4\u8d85\u8fc7\u4e00\u534a\u7684\u5143\u7d20\u79f0\u4e4b\u4e3a\u4e3b\u8981\u5143\u7d20\u3002\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u627e\u5230\u5b83\u7684\u4e3b\u8981\u5143\u7d20\u3002\u82e5\u6ca1\u6709\uff0c\u8fd4\u56de-1\u3002 \u793a\u4f8b 1\uff1a \u8f93&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/417"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=417"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":418,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions\/418"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}