题目描述
数组中占比超过一半的元素称为“主要元素”,也就是说,数组某个元素出现的次数大于数组长度的一半,该元素可称为主要元素。给定一个整型数组,找到它的主要元素,如果没有则返回-1。
解决思路
双指针法
画图示意一下
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">int</span> <span style="color:#3d8fd1">majorElement</span><span style="color:#999999">(</span><span style="color:#6679cc">int</span><span style="color:#999999">[</span><span style="color:#999999">]</span> arr<span style="color:#999999">)</span><span style="color:#999999">{</span>
<span style="color:#6b7394">//对于此题而言,把握好i和j的边界非常关键</span>
<span style="color:#6679cc">for</span> <span style="color:#999999">(</span><span style="color:#6679cc">int</span> i <span style="color:#ac9739">=</span> <span style="color:#ac9739">0</span><span style="color:#999999">;</span> i <span style="color:#ac9739"><=</span> arr<span style="color:#999999">.</span>length<span style="color:#ac9739">/</span><span style="color:#ac9739">2</span><span style="color:#999999">;</span> i<span style="color:#ac9739">++</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">for</span> <span style="color:#999999">(</span><span style="color:#6679cc">int</span> j <span style="color:#ac9739">=</span> arr<span style="color:#999999">.</span>length<span style="color:#ac9739">/</span><span style="color:#ac9739">2</span><span style="color:#999999">;</span> j <span style="color:#ac9739"><</span> arr<span style="color:#999999">.</span>length<span style="color:#999999">;</span> j<span style="color:#ac9739">++</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">if</span><span style="color:#999999">(</span>arr<span style="color:#999999">[</span>i<span style="color:#999999">]</span> <span style="color:#ac9739">==</span> arr<span style="color:#999999">[</span>j<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
<span style="color:#6679cc">return</span> arr<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#6679cc">return</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">1</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">int</span><span style="color:#999999">[</span><span style="color:#999999">]</span> arr1 <span style="color:#ac9739">=</span> <span style="color:#999999">{</span><span style="color:#ac9739">4</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">10</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">}</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"数组中的主要元素为: "</span><span style="color:#ac9739">+</span><span style="color:#3d8fd1">majorElement</span><span style="color:#999999">(</span>arr1<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
运行结果如下: