leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

build_arr_permutation.py (614B)


# 1920. Build Array from Permutation

"""
given a 0-based permutation 'nums', build an array 'ans' of the same length
where 'ans[i] = nums[nums[i]]' for each '0 <= i < nums.len' and return it. a
0-based permutation 'nums' is an array of distinct integers from 0 to
'nums.len - 1'.
"""


class Solution(object):
    def buildArray(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        return [nums[i] for i in nums]


if __name__ == "__main__":
    obj = Solution()
    print(obj.buildArray(nums=[0, 2, 1, 5, 3, 4]))
    print(obj.buildArray(nums=[5, 0, 1, 2, 3, 4]))