leet-code/src/main/java/leetcode/editor/cn/FindMajorityElementLcci.md

22 lines
774 B
Markdown
Raw Normal View History

2021-07-09 13:19:02 +08:00
<p>数组中占比超过一半的元素称之为主要元素。给你一个<strong> 整数 </strong>数组,找出其中的主要元素。若没有,返回 <code>-1</code> 。请设计时间复杂度为 <code>O(N)</code> 、空间复杂度为 <code>O(1)</code> 的解决方案。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>[1,2,5,9,5,9,5,5,5]
<strong>输出:</strong>5</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>[3,2]
<strong>输出:</strong>-1</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>[2,2,1,1,1,2,2]
<strong>输出:</strong>2</pre>
<div><div>Related Topics</div><div><li>数组</li><li>计数</li></div></div>\n<div><li>👍 134</li><li>👎 0</li></div>