1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/找出数组中的幸运数 [find-lucky-integer-in-an-array].html
2022-03-29 12:43:11 +08:00

53 lines
1.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>在整数数组中,如果一个整数的出现频次和它的数值大小相等,我们就称这个整数为「幸运数」。</p>
<p>给你一个整数数组 <code>arr</code>,请你从中找出并返回一个幸运数。</p>
<ul>
<li>如果数组中存在多个幸运数,只需返回 <strong>最大</strong> 的那个。</li>
<li>如果数组中不含幸运数,则返回 <strong>-1 </strong></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>arr = [2,2,3,4]
<strong>输出:</strong>2
<strong>解释:</strong>数组中唯一的幸运数是 2 ,因为数值 2 的出现频次也是 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>arr = [1,2,2,3,3,3]
<strong>输出:</strong>3
<strong>解释:</strong>1、2 以及 3 都是幸运数,只需要返回其中最大的 3 。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>arr = [2,2,2,3,3]
<strong>输出:</strong>-1
<strong>解释:</strong>数组中不存在幸运数。
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>arr = [5]
<strong>输出:</strong>-1
</pre>
<p><strong>示例 5</strong></p>
<pre><strong>输入:</strong>arr = [7,7,7,7,7,7,7]
<strong>输出:</strong>7
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= arr.length &lt;= 500</code></li>
<li><code>1 &lt;= arr[i] &lt;= 500</code></li>
</ul>