Leetcode / 1920. Build Array from Permutation
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution
{
int[] buildArray(int[] nums)
{
int[] arr = new int[nums.length];
for (int i = nums.length; i > 0; --i)
{
arr[i - 1] = nums[nums[i - 1]];
}
return arr;
}
void test()
{
int[][] testInputs = {
{0, 2, 1, 5, 3, 4},
{0, 1, 2, 4, 5, 3}
};
int[][] testOutputs = {
{0, 1, 2, 4, 5, 3},
{4, 5, 0, 1, 2, 3}
};
for (int i = 0; i < testInputs.length; i++) {
int[] testInput = testInputs[i];
int[] testOutput = testOutputs[i];
int[] testcase = buildArray(testInput);
for (int j = 0; j < testInput.length; j++) {
assert testcase[j] == testOutput[j];
}
}
}
public static void main(String[] args)
{
new Solution().test();
}
}
Did you like the lesson? 😆👍
Consider a donation to support our work: