mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			33 lines
		
	
	
		
			951 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			951 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,返回使所有数组元素相等需要的最小操作数。</p>
 | 
						||
 | 
						||
<p>在一次操作中,你可以使数组中的一个元素加 <code>1</code> 或者减 <code>1</code> 。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>nums = [1,2,3]
 | 
						||
<strong>输出:</strong>2
 | 
						||
<strong>解释:</strong>
 | 
						||
只需要两次操作(每次操作指南使一个元素加 1 或减 1):
 | 
						||
[<strong><em>1</em></strong>,2,3]  =>  [2,2,<strong><em>3</em></strong>]  =>  [2,2,2]
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>nums = [1,10,2,9]
 | 
						||
<strong>输出:</strong>16
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>n == nums.length</code></li>
 | 
						||
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
 | 
						||
</ul>
 |