LeetCode 1389. 按既定顺序创建目标数组
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 1389. 按既定顺序创建目标数组
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 題目
給你兩個(gè)整數(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 中都沒(méi)有要讀取的元素。
請(qǐng)你返回目標(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]示例 2: 輸入:nums = [1,2,3,4,0], index = [0,1,2,3,0] 輸出:[0,1,2,3,4] 解釋: nums index target 1 0 [1] 2 1 [1,2] 3 2 [1,2,3] 4 3 [1,2,3,4] 0 0 [0,1,2,3,4]示例 3: 輸入:nums = [1], index = [0] 輸出:[1]提示: 1 <= nums.length, index.length <= 100 nums.length == index.length 0 <= nums[i] <= 100 0 <= index[i] <= i來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/create-target-array-in-the-given-order
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
2. 解題
class Solution { public:vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {vector<int> ans;for(int i = 0; i < nums.size(); ++i){ans.insert(ans.begin()+index[i], nums[i]);}return ans;} };總結(jié)
以上是生活随笔為你收集整理的LeetCode 1389. 按既定顺序创建目标数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode 958. 二叉树的完全
- 下一篇: LeetCode 515. 在每个树行中