Algorithm/LeetCode

1920. Build Array from Permutation

JunGi Jeong 2023. 3. 30. 16:57

순열로 이루어진 배열 재구성하는 문제

https://leetcode.com/problems/build-array-from-permutation/description/

0부터 시작하는 순열로 이루어진 배열 nums가 주어진다.
이와 동일한 길이를 가지는 ans 배열을 반환하라.
ans[i] = nums[nums[i]]
class Solution {
    public int[] buildArray(int[] nums) {
        int len = nums.length;
        int[] ans = new int[len];
        for(int idx = 0; idx < len; idx++){
            ans[idx] = nums[nums[idx]];
        }
        return ans;
    }
}