mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个整数数组 <code>nums</code>,返回 <code>nums</code> 中最长等差子序列的<strong>长度</strong>。</p>
 | 
						||
 | 
						||
<p>回想一下,<code>nums</code> 的子序列是一个列表 <code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], ..., nums[i<sub>k</sub>]</code> ,且 <code>0 <= i<sub>1</sub> < i<sub>2</sub> < ... < i<sub>k</sub> <= nums.length - 1</code>。并且如果 <code>seq[i+1] - seq[i]</code>( <code>0 <= i < seq.length - 1</code>) 的值都相同,那么序列 <code>seq</code> 是等差的。</p>
 | 
						||
 | 
						||
<p> </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> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>2 <= nums.length <= 1000</code></li>
 | 
						||
	<li><code>0 <= nums[i] <= 500</code></li>
 | 
						||
</ul>
 |