mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			37 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given a <strong>zero-based permutation</strong> <code>nums</code> (<strong>0-indexed</strong>), build an array <code>ans</code> of the <strong>same length</strong> where <code>ans[i] = nums[nums[i]]</code> for each <code>0 <= i < nums.length</code> and return it.</p>
 | 
						|
 | 
						|
<p>A <strong>zero-based permutation</strong> <code>nums</code> is an array of <strong>distinct</strong> integers from <code>0</code> to <code>nums.length - 1</code> (<strong>inclusive</strong>).</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [0,2,1,5,3,4]
 | 
						|
<strong>Output:</strong> [0,1,2,4,5,3]<strong>
 | 
						|
Explanation:</strong> The array ans is built as follows: 
 | 
						|
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
 | 
						|
    = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
 | 
						|
    = [0,1,2,4,5,3]</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [5,0,1,2,3,4]
 | 
						|
<strong>Output:</strong> [4,5,0,1,2,3]
 | 
						|
<strong>Explanation:</strong> The array ans is built as follows:
 | 
						|
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
 | 
						|
    = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
 | 
						|
    = [4,5,0,1,2,3]</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= nums.length <= 1000</code></li>
 | 
						|
	<li><code>0 <= nums[i] < nums.length</code></li>
 | 
						|
	<li>The elements in <code>nums</code> are <strong>distinct</strong>.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Follow-up:</strong> Can you solve it without using an extra space (i.e., <code>O(1)</code> memory)?</p>
 |