{"id":1904,"date":"2016-03-23T22:27:55","date_gmt":"2016-03-23T14:27:55","guid":{"rendered":"http:\/\/boweihe.me\/?p=1904"},"modified":"2016-03-23T22:27:55","modified_gmt":"2016-03-23T14:27:55","slug":"leetcode-169-majority-element-my-submissions-question","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1904","title":{"rendered":"LeetCode 169. Majority Element My Submissions Question"},"content":{"rendered":"<p>Given an array of size\u00a0<i>n<\/i>, find the majority element. The majority element is the element that appears\u00a0more than\u00a0<code>\u230a n\/2 \u230b<\/code>\u00a0times.<br \/>\nYou may assume that the array is non-empty and the majority element always exist in the array.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u591a\u6570\u6295\u7968\u561b\uff0c\u8fd9\u4e2a\u9898\u76ee\u5f53\u5e74NB\u54c4\u54c4\u7684\u6570\u636e\u5e93\u8001\u5e08\u5728\u8bfe\u5802\u4e0a\u63d0\u5230\u8fc7\uff0c\u81f3\u4eca\u5370\u8c61\u6df1\u523b\uff0c\u8ddf\u6211\u4eec\u8bf4\u9009\u73ed\u957f\u7684\u65f6\u5019\u753b\u6b63\u5b57\u5c31\u662f\u5728\u6d6a\u8d39\u7a7a\u95f4\uff08\u5f53\u7136\u8981\u4fdd\u8bc1\u534a\u6570\u901a\u8fc7\u7684\u524d\u63d0\uff09\u3002<br \/>\n\u5047\u8bbe\u5f53\u524d\u5019\u9009\u4ebaA\u5531\u4e86\u4e09\u7968\uff0c\u90a3\u4e48\u5c0f\u9ed1\u677f\u4e0aA\u7684\u201c\u6b63\u201d\u5b57\u8bb0\u4e86\u4e09\u7b14\uff1b\u8fd9\u65f6\u5019\u6709\u4e86B\u7684\u4e00\u7968\uff0c\u7531\u4e8e\u6211\u4eec\u53ea\u8981\u7edf\u8ba1\u6392\u540d\u7b2c\u4e00\u7684\u4eba\uff0c\u6b64\u65f6\u53ef\u4ee5\u628aA\u7684\u6b63\u5b57\u5212\u53bb\u4e00\u7968\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u8bb0\u4e00\u4e2a\u201cB\uff0c\u4e00\u7968\u201d\u3002\u56e0\u4e3a\u7167\u8fd9\u6837\u8ba1\u7b97\uff0c\u5982\u679cB\u518d\u5531\u4e86\u4e24\u7968\uff0c\u90a3\u4e24\u4e2a\u4eba\u7684\u7968\u521a\u597d\u62b5\u6d88\uff0c\u5c0f\u9ed1\u677f\u88ab\u64e6\u5e72\u51c0\uff0c\u8fd9\u65f6\u5019\u4e0d\u7ba1\u662f\u8c01\u5f97\u4e86\u4e00\u7968\u5c31\u5199\u5728\u5c0f\u9ed1\u677f\u4e0a\u91cd\u65b0\u8ba1\u6570\u5566<\/p>\n<h3>\u601d\u8def2<\/h3>\n<p>\u8bbe\u60f3\u4e00\u4e0b\u6392\u5e8f\u540e\u7684\u6570\u5217\uff0c\u5982\u679c\u6709\u4e2a\u5143\u7d20\u5360\u4e86\u8d85\u8fc7\u534a\u6570\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5b83\u80af\u5b9a\u4e5f\u662f\u8fd9\u4e2a\u6570\u5217\u7684\u4e2d\u4f4d\u6570\u3002\u53c2\u8003\u5feb\u901f\u6392\u5e8f\u7684\u601d\u60f3\uff0c\u53ef\u4ee5\u5728O(n)\u7684\u590d\u6742\u5ea6\u627e\u5230\u8fd9\u4e2a\u4e2d\u4f4d\u6570\uff0c\u5b83\u4e5f\u5c31\u662f\u8981\u5f97\u5230\u7684\u7ed3\u679c\u3002\uff08\u53c2\u8003<a href=\"http:\/\/boweihe.me\/?p=1879\" target=\"_blank\" rel=\"noopener noreferrer\">\u6570\u7ec4\u7b2ck\u5927\u6570\u7684\u90a3\u4e2a\u9012\u5f52\u65b9\u6cd5<\/a>\uff09<\/p>\n<h3>\u4ee3\u7801\uff08\u601d\u8def1\uff09<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int majorityElement(vector&lt;int&gt;&amp; nums) {\n        int majNum = -1;\n        int majCount = 0;\n        int numSz = nums.size();\n        for(int i=0; i&lt;numSz; i++){\n            if(majNum != nums[i]){\n                if(majCount == 0){\n                    majNum = nums[i];\n                    majCount = 1;\n                } else {\n                    majCount--;\n                }\n            } else {\n                majCount++;\n            }\n        }\n        return majNum;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of size\u00a0n, find the majority element. The majority element is the element that appears\u00a0more than\u00a0\u230a n\/2 \u230b\u00a0times. You may assume that the array is non-empty and the majority element always exist in the array. \u601d\u8def \u591a\u6570\u6295\u7968\u561b\uff0c\u8fd9\u4e2a\u9898\u76ee\u5f53\u5e74NB\u54c4\u54c4\u7684\u6570\u636e\u5e93\u8001\u5e08\u5728\u8bfe\u5802\u4e0a\u63d0\u5230\u8fc7\uff0c\u81f3\u4eca\u5370\u8c61\u6df1\u523b\uff0c\u8ddf\u6211\u4eec\u8bf4\u9009\u73ed\u957f\u7684\u65f6\u5019\u753b\u6b63\u5b57\u5c31\u662f\u5728\u6d6a\u8d39\u7a7a\u95f4\uff08\u5f53\u7136\u8981\u4fdd\u8bc1\u534a\u6570\u901a\u8fc7\u7684\u524d\u63d0\uff09\u3002 \u5047\u8bbe\u5f53\u524d\u5019\u9009\u4ebaA\u5531\u4e86\u4e09\u7968\uff0c\u90a3\u4e48\u5c0f\u9ed1\u677f\u4e0aA\u7684\u201c\u6b63\u201d\u5b57\u8bb0\u4e86\u4e09\u7b14\uff1b\u8fd9\u65f6\u5019\u6709\u4e86B\u7684\u4e00\u7968\uff0c\u7531\u4e8e\u6211\u4eec\u53ea\u8981\u7edf\u8ba1\u6392\u540d\u7b2c\u4e00\u7684\u4eba\uff0c\u6b64\u65f6\u53ef\u4ee5\u628aA\u7684\u6b63\u5b57\u5212\u53bb\u4e00\u7968\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u8bb0\u4e00\u4e2a\u201cB\uff0c\u4e00\u7968\u201d\u3002\u56e0\u4e3a\u7167\u8fd9\u6837\u8ba1\u7b97\uff0c\u5982\u679cB\u518d\u5531\u4e86\u4e24\u7968\uff0c\u90a3\u4e24\u4e2a\u4eba\u7684\u7968\u521a\u597d\u62b5\u6d88\uff0c\u5c0f\u9ed1\u677f\u88ab\u64e6\u5e72\u51c0\uff0c\u8fd9\u65f6\u5019\u4e0d\u7ba1\u662f\u8c01\u5f97\u4e86\u4e00\u7968\u5c31\u5199\u5728\u5c0f\u9ed1\u677f\u4e0a\u91cd\u65b0\u8ba1\u6570\u5566 \u601d\u8def2 \u8bbe\u60f3\u4e00\u4e0b\u6392\u5e8f\u540e\u7684\u6570\u5217\uff0c\u5982\u679c\u6709\u4e2a\u5143\u7d20\u5360\u4e86\u8d85\u8fc7\u534a\u6570\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5b83\u80af\u5b9a\u4e5f\u662f\u8fd9\u4e2a\u6570\u5217\u7684\u4e2d\u4f4d\u6570\u3002\u53c2\u8003\u5feb\u901f\u6392\u5e8f\u7684\u601d\u60f3\uff0c\u53ef\u4ee5\u5728O(n)\u7684\u590d\u6742\u5ea6\u627e\u5230\u8fd9\u4e2a\u4e2d\u4f4d\u6570\uff0c\u5b83\u4e5f\u5c31\u662f\u8981\u5f97\u5230\u7684\u7ed3\u679c\u3002\uff08\u53c2\u8003\u6570\u7ec4\u7b2ck\u5927\u6570\u7684\u90a3\u4e2a\u9012\u5f52\u65b9\u6cd5\uff09 \u4ee3\u7801\uff08\u601d\u8def1\uff09 class Solution { public: int majorityElement(vector&lt;int&gt;&amp; nums) { int majNum = -1; [&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],"class_list":["post-1904","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1904","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=1904"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1904\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}