#1920
Easy Algorithms Build array from permutation
Array Simulation
91.1% acceptance
Feb 25, 2026
4021
488
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.
A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn build_array(nums: Vec<i32>) -> Vec<i32> {
nums.iter().map(|&i| nums[i as usize]).collect()
}
}