Your Cart

Leetcode 1920. Build Array from Permutation in Golang

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

We need to loop through the nums array and fill in a new array in the proper order.


func buildArray(nums []int) []int {
    // Create a new array of the same size as nums
    ans := make([]int, len(nums))
    // Loop through nums
    for index, _ := range nums {
        // Fill in the new array with the proper value from nums,
        // taken from the question
        ans[index] = nums[nums[index]]
    }
    return ans
}
SHARE: