[Java][Array][LeetCode] Create Target Array in the Given Order #1389

SP Hou
Aug 5, 2021

Given two arrays of integers nums and index. Your task is to create target array under the following rules:

  • Initially target array is empty.
  • From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
  • Repeat the previous step until there are no elements to read in nums and index.

Return the target array.

It is guaranteed that the insertion operations will be valid.

Example 1:

Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output: [0,4,1,3,2]

Two methods: Array and List. Those are faster than 100%.
If use Array, I need move the value and depend on index value by myself. But, List can insert directly.
可以用Array跟List兩種做法,
用Array的方式,則要自己移動位置,用List的方式,則不需要自己移動,直接把數值插在要的位置就可以了。

--

--