Your Cart

Leetcode 1920. Build Array from Permutation in Golang

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

Leave a Reply

Your email address will not be published. Required fields are marked *