mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,数组长度为 <strong>偶数</strong> ,由数目相等的正整数和负整数组成。</p>
 | 
						||
 | 
						||
<p>你需要 <strong>重排</strong> <code>nums</code> 中的元素,使修改后的数组满足下述条件:</p>
 | 
						||
 | 
						||
<ol>
 | 
						||
	<li>任意 <strong>连续</strong> 的两个整数 <strong>符号相反</strong></li>
 | 
						||
	<li>对于符号相同的所有整数,<strong>保留</strong> 它们在 <code>nums</code> 中的 <strong>顺序</strong> 。</li>
 | 
						||
	<li>重排后数组以正整数开头。</li>
 | 
						||
</ol>
 | 
						||
 | 
						||
<p>重排元素满足上述条件后,返回修改后的数组。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>nums = [3,1,-2,-5,2,-4]
 | 
						||
<strong>输出:</strong>[3,-2,1,-5,2,-4]
 | 
						||
<strong>解释:</strong>
 | 
						||
nums 中的正整数是 [3,1,2] ,负整数是 [-2,-5,-4] 。
 | 
						||
重排的唯一可行方案是 [3,-2,1,-5,2,-4],能满足所有条件。
 | 
						||
像 [1,-2,2,-5,3,-4]、[3,1,2,-2,-5,-4]、[-2,3,-5,1,-4,2] 这样的其他方案是不正确的,因为不满足一个或者多个条件。 
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>nums = [-1,1]
 | 
						||
<strong>输出:</strong>[1,-1]
 | 
						||
<strong>解释:</strong>
 | 
						||
1 是 nums 中唯一一个正整数,-1 是 nums 中唯一一个负整数。
 | 
						||
所以 nums 重排为 [1,-1] 。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>2 <= nums.length <= 2 * 10<sup>5</sup></code></li>
 | 
						||
	<li><code>nums.length</code> 是 <strong>偶数</strong></li>
 | 
						||
	<li><code>1 <= |nums[i]| <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>nums</code> 由 <strong>相等</strong> 数量的正整数和负整数组成</li>
 | 
						||
</ul>
 |