mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个整数数组 <code>arr</code> 和一个整数 <code>difference</code>,请你找出并返回 <code>arr</code> 中最长等差子序列的长度,该子序列中相邻元素之间的差等于 <code>difference</code> 。</p>
 | 
						||
 | 
						||
<p><strong>子序列</strong> 是指在不改变其余元素顺序的情况下,通过删除一些元素或不删除任何元素而从 <code>arr</code> 派生出来的序列。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>arr = [1,2,3,4], difference = 1
 | 
						||
<strong>输出:</strong>4
 | 
						||
<strong>解释:</strong>最长的等差子序列是 [1,2,3,4]。</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>arr = [1,3,5,7], difference = 1
 | 
						||
<strong>输出:</strong>1
 | 
						||
<strong>解释:</strong>最长的等差子序列是任意单个元素。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 3:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>arr = [1,5,7,8,5,3,4,2,1], difference = -2
 | 
						||
<strong>输出:</strong>4
 | 
						||
<strong>解释:</strong>最长的等差子序列是 [7,5,3,1]。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>-10<sup>4</sup> <= arr[i], difference <= 10<sup>4</sup></code></li>
 | 
						||
</ul>
 |