Description
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).
Example
|
|
{: file="Input/Output”}
Thought Process
Simple Brute force method that requires $O(n)$ time and space complexity.\
Using the explanation, Ive created a new array (ans
) and assigned the elements of ans
with the corrosponding
values from nums[nums[i]]
, where i is the index of the for loop.
Code
|
|
{: file="Solution.java” }
Afterthoughts
This could be improved to $O(1)$ time and space complexity but I need more experience with data structures
and algorithms. This is also my first Java leetcode attempt.