Skip to content

Commit c270100

Browse files
Add files via upload
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] Explanation: 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]
1 parent 02bb478 commit c270100

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
4+
vector<int>ans;
5+
for(int i=0;i<nums.size();i++){
6+
//insert(index,value)
7+
ans.insert(ans.begin()+index[i],nums[i]);
8+
}
9+
return ans;
10+
}
11+
};

0 commit comments

Comments
 (0)