1920. Build Array from Permutation Easy

@problem@discussion
#Array#Simulation



1/**
2 * [1920] Build Array from Permutation
3 *
4 * Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
5 * A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [0,2,1,5,3,4]
10 * Output: [0,1,2,4,5,3]
11 * Explanation: The array ans is built as follows: 
12 * ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
13 *     = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
14 *     = [0,1,2,4,5,3]
15 * Example 2:
16 * 
17 * Input: nums = [5,0,1,2,3,4]
18 * Output: [4,5,0,1,2,3]
19 * Explanation: The array ans is built as follows:
20 * ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
21 *     = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
22 *     = [4,5,0,1,2,3]
23 *  
24 * Constraints:
25 * 
26 * 	1 <= nums.length <= 1000
27 * 	0 <= nums[i] < nums.length
28 * 	The elements in nums are distinct.
29 * 
30 *  
31 * Follow-up: Can you solve it without using an extra space (i.e., O(1) memory)?
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/build-array-from-permutation/
37// discuss: https://leetcode.com/problems/build-array-from-permutation/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn build_array(nums: Vec<i32>) -> Vec<i32> {
43        vec![]
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_1920() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.