给定两个整数数组arr和大小为N 的索引,任务是通过在索引数组给定的索引处插入arr数组中给定的元素来创建一个新数组。如果特定位置出现多次,则右移右侧数组元素,然后在给定索引处插入元素。
注意:假设索引数组中的所有索引都在 [0, N) 范围内。
例子:
Input: arr = [0, 1, 2, 3, 4], index = [0, 1, 2, 2, 1]
Output: [0, 4, 1, 3, 2]
Explanation:
First we insert at 0th, 1st and 2nd index so the array will become [0, 1, 2]
Then we insert again at 2nd position and rightshift all rightside elements so array will be [0, 1, 3, 2]
Then we insert 4 at first index so final array will become: [0, 4, 1, 3, 2].
Input: arr = [1, 2, 3, 4, 0], index = [0, 1, 2, 3, 0]
Output: [0, 1, 2, 3, 4]
方法(使用静态数组):
如果我们使用静态数组,那么可以使用以下步骤解决给定的问题:
- 创建一个大小为N的新数组finalArr ,以存储结果输出。
- 对于给定arr数组中的每个元素,将其插入到索引数组给出的相应给定索引处,只需使用:
finalArr[index[i]] = arr[i]
where i is the current
position during iteration
- 如果索引重复,则右移所有右侧元素,最后在该位置插入。
方法(使用动态数组):
如果我们使用类似动态数组的结构,比如 Vectors 等;那么我们可以简单地在给定的索引处插入给定的元素,而不必担心重复。当它在每次插入时扩展时,类似向量的数据结构会自行处理右移。
- 创建一个新向量vec ,以存储结果输出。
- 对于给定arr数组中的每个元素,只需使用 vector 的 insert()函数,将其插入到索引数组给出的相应给定索引处。这将插入特定位置,并自动处理重复位置。
下面是上述方法的实现:
C++
// C++ program to reorder an Array
// according to given indices
// with repetition allowed
#include
using namespace std;
// Function that returns the
// modified vector according to indices
vector createTargetArray(
vector& nums, vector& index)
{
// create an ans vector
vector ans;
int n = nums.size();
for (int i = 0; i < n; i++)
// insert at particular position
// mention in index array.
ans.insert(ans.begin() + index[i],
nums[i]);
// finally return ans
return ans;
}
// Function to reorder and
// print the given array
void reorder(
vector& arr, vector& index)
{
vector ans;
ans = createTargetArray(arr, index);
// print ans vector
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
vector arr = { 0, 1, 2, 3, 4 };
vector index = { 0, 1, 2, 2, 1 };
reorder(arr, index);
return 0;
}
Java
// Java program to reorder an Array
// according to given indices
// with repetition allowed
import java.util.*;
class GFG {
// Function that returns the
// modified List according to indices
static List createTargetArray(List nums,
List index)
{
// Create an ans List
List ans = new ArrayList<>();
int n = nums.size();
for(int i = 0; i < n; i++)
{
// Insert at particular position
// mention in index array
ans.add(index.get(i), nums.get(i));
}
// Finally return ans
return ans;
}
// Function to reorder and
// print the given array
static void reorder(List arr,
List index)
{
List ans;
ans = createTargetArray(arr, index);
// Print ans list
for(int i = 0; i < ans.size(); i++)
{
System.out.print(ans.get(i) + " ");
}
}
// Driver code
public static void main(String[] args)
{
List arr = Arrays.asList(0, 1, 2, 3, 4);
List index = Arrays.asList(0, 1, 2, 2, 1);
reorder(arr, index);
}
}
// This code is contributed by offbeat
Python3
# Python3 program to reorder an array
# according to given indices
# with repetition allowed
# Function that returns the modified
# vector according to indices
def createTargetArray(nums, index):
# Create an ans vector
ans = []
n = len(nums)
for i in range(n):
# Insert at particular position
# mention in index array.
ans.insert(index[i], nums[i])
# Finally return ans
return ans
# Function to reorder and
# print the given array
def reorder(arr, index):
ans = createTargetArray(arr, index)
# Print ans vector
for i in range(len(ans)):
print(ans[i], end = " ")
# Driver Code
if __name__ == "__main__":
arr = [ 0, 1, 2, 3, 4 ]
index = [ 0, 1, 2, 2, 1 ]
reorder(arr, index)
# This code is contributed by chitranayal
C#
// C# program to reorder an Array
// according to given indices
// with repetition allowed
using System;
using System.Collections.Generic;
class GFG{
// Function that returns the
// modified List according to indices
static List createTargetArray(List nums,
List index)
{
// Create an ans List
List ans = new List();
int n = nums.Count;
for(int i = 0; i < n; i++)
{
// Insert at particular position
// mention in index array
ans.Insert(index[i], nums[i]);
}
// Finally return ans
return ans;
}
// Function to reorder and
// print the given array
static void reorder(List arr,
List index)
{
List ans;
ans = createTargetArray(arr, index);
// Print ans list
for(int i = 0; i < ans.Count; i++)
{
Console.Write(ans[i] + " ");
}
}
// Driver code
public static void Main()
{
List arr = new List{ 0, 1, 2, 3, 4 };
List index = new List{ 0, 1, 2, 2, 1 };
reorder(arr, index);
}
}
// This code is contributed by akhilsaini
Javascript
0 4 1 3 2
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live