[LeetCode] Build Array from Permutation

1920. Build Array from Permutation

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).

1
2
3
4
5
6
7
8
class Solution {
public:
vector<int> buildArray(vector<int>& nums) {
vector<int> res(nums.size());
for(int i = 0; i < nums.size(); i++) res[i] = nums[nums[i]];
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/05/06/PS/LeetCode/build-array-from-permutation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.