mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			50 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个整数数组 <code>nums</code> 。每一次操作中,你可以将 <code>nums</code> 中 <strong>任意</strong> 一个元素替换成 <strong>任意 </strong>整数。</p>
 | 
						||
 | 
						||
<p>如果 <code>nums</code> 满足以下条件,那么它是 <strong>连续的</strong> :</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>nums</code> 中所有元素都是 <b>互不相同</b> 的。</li>
 | 
						||
	<li><code>nums</code> 中 <strong>最大</strong> 元素与 <strong>最小</strong> 元素的差等于 <code>nums.length - 1</code> 。</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p>比方说,<code>nums = [4, 2, 5, 3]</code> 是 <strong>连续的</strong> ,但是 <code>nums = [1, 2, 3, 5, 6]</code> <strong>不是连续的</strong> 。</p>
 | 
						||
 | 
						||
<p>请你返回使 <code>nums</code> <strong>连续</strong> 的 <strong>最少</strong> 操作次数。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre><b>输入:</b>nums = [4,2,5,3]
 | 
						||
<b>输出:</b>0
 | 
						||
<b>解释:</b>nums 已经是连续的了。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre><b>输入:</b>nums = [1,2,3,5,6]
 | 
						||
<b>输出:</b>1
 | 
						||
<b>解释:</b>一个可能的解是将最后一个元素变为 4 。
 | 
						||
结果数组为 [1,2,3,5,4] ,是连续数组。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 3:</strong></p>
 | 
						||
 | 
						||
<pre><b>输入:</b>nums = [1,10,100,1000]
 | 
						||
<b>输出:</b>3
 | 
						||
<b>解释:</b>一个可能的解是:
 | 
						||
- 将第二个元素变为 2 。
 | 
						||
- 将第三个元素变为 3 。
 | 
						||
- 将第四个元素变为 4 。
 | 
						||
结果数组为 [1,2,3,4] ,是连续数组。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
 | 
						||
</ul>
 |