{"id":35624,"date":"2024-11-26T09:56:49","date_gmt":"2024-11-26T01:56:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35624\/"},"modified":"2024-11-26T09:56:49","modified_gmt":"2024-11-26T01:56:49","slug":"leetcode-%e9%a1%b6%e7%ba%a7%e8%ae%bf%e8%b0%88-%e5%a4%9a%e6%95%b0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35624\/","title":{"rendered":"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u591a\u6570\u5143\u7d20"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173067877768716.jpg\" class=\"aligncenter\" title=\"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u591a\u6570\u5143\u7d20\u63d2\u56fe\" alt=\"Leetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u591a\u6570\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\uff0c\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n  \u7ed9\u5b9a\u4e00\u4e2a\u5927\u5c0f\u4e3a n \u7684\u6570\u7ec4 nums\uff0c\u8fd4\u56de\u591a\u6570\u5143\u7d20\u3002 \u591a\u6570\u5143\u7d20\u662f\u51fa\u73b0\u8d85\u8fc7 \u230an\/2\u230b \u6b21\u7684\u5143\u7d20\u3002\u60a8\u53ef\u4ee5\u5047\u8bbe\u591a\u6570\u5143\u7d20\u59cb\u7ec8\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1anums = [3,2,3] \u8f93\u51fa\uff1a3 \u793a\u4f8b2\uff1a \u8f93\u5165\uff1anums = [2,2,1,1,1,2,2] \u8f93\u51fa\uff1a2 \u9650\u5236\uff1a n == nums.length 1\n<\/p><\/blockquote>\n<p>\u4e4d\u4e00\u770b\uff0c\u60a8\u4f1a\u8003\u8651\u5236\u4f5c\u4e00\u5f20\u5730\u56fe\uff0c\u7136\u540e\u6536\u96c6\u663e\u793a\u6700\u591a\u7684\u5730\u56fe\u3002<\/p>\n<p>\u518d\u60f3\u4e00\u60f3\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u6392\u5e8f\u5e76\u627e\u5230\u51fa\u73b0\u6700\u591a\u7684\u90a3\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5982\u679c\u60a8\u4ed4\u7ec6\u9605\u8bfb\u8bf4\u660e\uff0c\u60a8\u4f1a\u660e\u767d\u591a\u6570\u5143\u7d20\u662f\u6307\u51fa\u73b0\u8d85\u8fc7\u6570\u7ec4\u4e00\u534a\u7684\u5143\u7d20\u3002<\/p>\n<p>\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u5982\u679c\u4f60\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u5e76\u83b7\u53d6\u4e2d\u95f4\u7684\u7d22\u5f15\uff0c\u90a3\u5c31\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff1a<\/p>\n<pre>class Solution {\n    public int majorityElement(int[] nums) {\n\n        \/\/ sort\n        Arrays.sort(nums);\n\n        \/\/ if by majority element it means that appears more than half of nums size\n        \/\/ then picking the middle element would be the one that's a majority element\n        return nums[nums.length \/ 2];\n    }\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<blockquote>\n<p>\u8fd0\u884c\u65f6\u95f4\uff1a4 \u6beb\u79d2\uff0c\u6bd4 54.53% \u7684 java \u5728\u7ebf\u63d0\u4ea4\u7684\u591a\u6570\u5143\u7d20\u8981\u5feb\u3002<\/p>\n<p>\u5185\u5b58\u4f7f\u7528\uff1a53.5 mb\uff0c\u5c11\u4e8e java \u5728\u7ebf\u63d0\u4ea4\u7684\u591a\u6570\u5143\u7d20\u7684 9.23%\u3002<\/p>\n<\/blockquote>\n<p>\u2014<\/p>\n<p>\u5c31\u662f\u8fd9\u6837\uff01\u5982\u679c\u8fd8\u6709\u4ec0\u4e48\u8981\u8ba8\u8bba\u7684\uff0c\u8bf7\u968f\u65f6\u53d1\u8868\u8bc4\u8bba\uff0c\u5982\u679c\u6211\u9519\u8fc7\u4e86\u4efb\u4f55\u5185\u5bb9\uff0c\u8bf7\u544a\u8bc9\u6211\uff0c\u4ee5\u4fbf\u6211\u8fdb\u884c\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n<p>\u76f4\u5230\u4e0b\u4e00\u7bc7\u6587\u7ae0\uff01 \ud83d\ude42<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLeetcode \u2014 \u9876\u7ea7\u8bbf\u8c08 \u2014 \u591a\u6570\u5143\u7d20\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\uff0c\u63cf\u8ff0\u5982\u4e0b\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u5927\u5c0f\u4e3a n \u7684\u6570\u7ec4 nums\uff0c\u8fd4\u56de\u591a\u6570\u5143\u7d20\u3002 \u591a\u6570\u5143\u7d20\u662f\u51fa\u73b0\u8d85\u8fc7 \u230an\/2\u230b \u6b21\u7684\u5143\u7d20\u3002\u60a8\u53ef\u4ee5\u5047\u8bbe\u591a\u6570\u5143\u7d20\u59cb\u7ec8\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1anums = [3,2,3] \u8f93\u51fa\uff1a3 \u793a\u4f8b2\uff1a \u8f93\u5165\uff1anums = [2,2,1,1,1,2,2] \u8f93\u51fa\uff1a2 \u9650\u5236\uff1a n == nums.length 1 \u4e4d\u4e00\u770b\uff0c\u60a8\u4f1a\u8003\u8651\u5236\u4f5c\u4e00\u5f20\u5730\u56fe\uff0c\u7136\u540e\u6536\u96c6\u663e\u793a\u6700\u591a\u7684\u5730\u56fe\u3002 \u518d\u60f3\u4e00\u60f3\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u6392\u5e8f\u5e76\u627e\u5230\u51fa\u73b0\u6700\u591a\u7684\u90a3\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd8\u6709\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5982\u679c\u60a8\u4ed4\u7ec6\u9605\u8bfb\u8bf4\u660e\uff0c\u60a8\u4f1a\u660e\u767d\u591a\u6570\u5143\u7d20\u662f\u6307\u51fa\u73b0\u8d85\u8fc7\u6570\u7ec4\u4e00\u534a\u7684\u5143\u7d20\u3002 \u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u5982\u679c\u4f60\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u5e76\u83b7\u53d6\u4e2d\u95f4\u7684\u7d22\u5f15\uff0c\u90a3\u5c31\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff1a class Solution { public int majorityElement(int[] nums) { \/\/ sort Arrays.sort(nums); \/\/ if by majority element it means that appears more than half of nums size \/\/ then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35624","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35624"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35624\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}