1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-03-14 16:22:24 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最长和谐子序列 [longest-harmonious-subsequence].html

53 lines
1.7 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>和谐数组是指一个数组里元素的最大值和最小值之间的差别 <strong>正好是 <code>1</code></strong></p>
<p>给你一个整数数组 <code>nums</code> ,请你在所有可能的 <span data-keyword="subsequence-array">子序列</span> 中找到最长的和谐子序列的长度。</p>
<p>数组的 <strong>子序列</strong> 是一个由数组派生出来的序列,它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [1,3,2,2,5,2,3,7]</span></p>
<p><span class="example-io"><b>输出:</b>5</span></p>
<p><strong>解释:</strong></p>
<p>最长和谐子序列是&nbsp;<code>[3,2,2,2,3]</code></p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4]</span></p>
<p><span class="example-io"><b>输出:</b>2</span></p>
<p><strong>解释:</strong></p>
<p>最长和谐子序列是&nbsp;<code>[1,2]</code><code>[2,3]</code>&nbsp;&nbsp;<code>[3,4]</code>,长度都为 2。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [1,1,1,1]</span></p>
<p><span class="example-io"><b>输出:</b>0</span></p>
<p><strong>解释:</strong></p>
<p>不存在和谐子序列。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>