給你兩個整數(shù)數(shù)組 nums 和 index。你需要按照以下規(guī)則創(chuàng)建目標(biāo)數(shù)組:
目標(biāo)數(shù)組 target 最初為空。
按從左到右的順序依次讀取 nums[i] 和 index[i]楞慈,在 target 數(shù)組中的下標(biāo) index[i] 處插入值 nums[i] 。
重復(fù)上一步,直到在 nums 和 index 中都沒有要讀取的元素嗡贺。
請你返回目標(biāo)數(shù)組。
題目保證數(shù)字插入位置總是存在鞍帝。
示例 1:
輸入:nums = [0,1,2,3,4], index = [0,1,2,2,1]
輸出:[0,4,1,3,2]
解釋:
nums? ? ? index? ? target
0? ? ? ? ? ? 0? ? ? ? [0]
1? ? ? ? ? ? 1? ? ? ? [0,1]
2? ? ? ? ? ? 2? ? ? ? [0,1,2]
3? ? ? ? ? ? 2? ? ? ? [0,1,3,2]
4? ? ? ? ? ? 1? ? ? ? [0,4,1,3,2]
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/create-target-array-in-the-given-order
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有诫睬。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處帕涌。
class?Solution?{
????public?int[]?createTargetArray(int[]?nums,?int[]?index)?{
????????List<Integer>?list?=?new?ArrayList<>();
????????for(int?i=0;i<nums.length;i++){
????????????list.add(index[i],nums[i]);
????????}
????????return?list.stream().mapToInt(Integer::valueOf).toArray();
????}
}