1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最长等差数列 [longest-arithmetic-subsequence].html
2022-03-29 12:43:11 +08:00

42 lines
1.3 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>给你一个整数数组&nbsp;<code>nums</code>,返回 <code>nums</code>&nbsp;中最长等差子序列的<strong>长度</strong></p>
<p>回想一下,<code>nums</code> 的子序列是一个列表&nbsp;<code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], ..., nums[i<sub>k</sub>]</code> ,且&nbsp;<code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k</sub> &lt;= nums.length - 1</code>。并且如果&nbsp;<code>seq[i+1] - seq[i]</code>(&nbsp;<code>0 &lt;= i &lt; seq.length - 1</code>) 的值都相同,那么序列&nbsp;<code>seq</code>&nbsp;是等差的。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [3,6,9,12]
<strong>输出:</strong>4
<strong>解释: </strong>
整个数组是公差为 3 的等差数列。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [9,4,7,2,10]
<strong>输出:</strong>3
<strong>解释:</strong>
最长的等差子序列是 [4,7,10]。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [20,1,15,3,10,5,8]
<strong>输出:</strong>4
<strong>解释:</strong>
最长的等差子序列是 [20,15,10,5]。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 1000</code></li>
<li><code>0 &lt;= nums[i] &lt;= 500</code></li>
</ul>