📌  相关文章
📜  根据给定的索引对数组进行重新排序,并允许重复

📅  最后修改于: 2021-05-14 02:25:29             🧑  作者: Mango

给定两个整数数组arr和大小为N的索引,任务是通过将arr数组中给定的元素插入到索引数组给定的索引处来创建一个新数组。如果某个特定位置多次出现,则右移右侧数组元素,然后在给定索引处插入元素。

注意:假定索引数组中的所有索引都在[0,N)范围内。

例子:

方法(使用静态数组):
如果我们使用静态数组,则可以使用以下步骤解决给定的问题:

  • 创建一个大小为N的新数组finalArr ,以存储结果输出。
  • 对于给定的arr数组中的每个元素,只需使用以下命令将其插入到索引数组给定的相应给定索引处即可:
finalArr[index[i]] = arr[i]

where i is the current
position during iteration

  • 如果重复索引,则右移所有右侧元素,然后最后在该位置插入。

方法(使用动态数组):
如果我们使用像数组这样的动态数组结构,例如Vectors等;那么我们可以简单地将给定元素插入给定索引,而不必担心重复。类似于矢量的数据结构在每次插入时扩展时都会自行进行右移。

  1. 创建一个新的向量vec ,以存储结果输出。
  2. 对于给定的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


输出:
0 4 1 3 2