给定一个数组arr[] ,任务是打印通过将arr[] 的元素一个一个地插入到一个最初为空的数组中获得的数组,比如arr1[] ,并在每次插入后反转数组arr1[] 。
例子:
Input: arr[] = {1, 2, 3, 4}
Output: 4 2 1 3
Explanation:
Operations performed on the array arr1[] as follows:
Step 1: Append 1 into the array and reverse it. arr1[] = {1}
Step 2: Append 2 into the array and reverse it. arr1[] = {2, 1}
Step 3: Append 3 into the array and reverse it. arr1[] = {3, 1, 2}
Step 3: Append 4 into the array and reverse it. arr1[] = {4, 2, 1, 3}
Input: arr[] = {1, 2, 3}
Output: 3 1 2
Explanation:
Operations performed on the array arr1[] as follows:
Step 1: Append 1 into the array and reverse it. arr1[] = {1}
Step 2: Append 2 into the array and reverse it. arr1[] = {2, 1}
Step 3: Append 3 into the array and reverse it. arr1[] = {3, 1, 2}
朴素的方法:解决问题最简单的方法是遍历数组arr[]并将arr[] 的每个元素一个一个地插入到数组arr1[] 中,并在每次插入后反转数组arr1[] 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:对上述方法进行优化,思路是使用双端队列(Deque)来追加两端的元素。请按照以下步骤解决问题:
- 初始化一个双端队列来存储转换后的数组的元素。
- 遍历数组arr[]的元素,如果元素的索引和元素的长度具有相同的奇偶校验,则将元素推送到 Deque 的前面。否则,将其推到 Deque 的后面。
- 最后,在数组arr[]的完整迭代之后,将 Deque 的内容打印为所需的输出排列。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to generate the array by
// inserting array elements one by one
// followed by reversing the array
void generateArray(int arr[], int n)
{
// Doubly ended Queue
deque ans;
// Iterate over the array
for (int i = 0; i < n; i++) {
// Push array elements
// alternately to the front
// and back
if (i & 1)
ans.push_front(arr[i]);
else
ans.push_back(arr[i]);
}
// If size of list is odd
if (n & 1) {
// Reverse the list
reverse(ans.begin(),
ans.end());
}
// Print the elements
// of the array
for (auto x : ans) {
cout << x << " ";
}
cout << endl;
}
// Driver Code
int32_t main()
{
int n = 4;
int arr[n] = { 1, 2, 3, 4 };
generateArray(arr, n);
return 0;
}
Java
// Java program of the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to generate the array by
// inserting array elements one by one
// followed by reversing the array
static void generateArray(int arr[], int n)
{
// Doubly ended Queue
Deque ans = new LinkedList<>();
// Iterate over the array
for(int i = 0; i < n; i++)
{
// Push array elements
// alternately to the front
// and back
if ((i & 1) != 0)
ans.addFirst(arr[i]);
else
ans.add(arr[i]);
}
// If size of list is odd
if ((n & 1) != 0)
{
// Reverse the list
Collections.reverse(Arrays.asList(ans));
}
// Print the elements
// of the array
for(int x : ans)
{
System.out.print(x + " ");
}
System.out.println();
}
// Driver Code
public static void main (String[] args)
{
int n = 4;
int arr[] = { 1, 2, 3, 4 };
generateArray(arr, n);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program of the above approach
from collections import deque
# Function to generate the array by
# inserting array elements one by one
# followed by reversing the array
def generateArray(arr, n):
# Doubly ended Queue
ans = deque()
# Iterate over the array
for i in range(n):
# Push array elements
# alternately to the front
# and back
if (i & 1 != 0):
ans.appendleft(arr[i])
else:
ans.append(arr[i])
# If size of list is odd
if (n & 1 != 0):
# Reverse the list
ans.reverse()
# Print the elements
# of the array
for x in ans:
print(x, end = " ")
print()
# Driver Code
n = 4
arr = [ 1, 2, 3, 4 ]
generateArray(arr, n)
# This code is contributed by code_hunt
C#
// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to generate the array
// by inserting array elements
// one by one followed by
// reversing the array
static void generateArray(int []arr,
int n)
{
// Doubly ended Queue
List ans = new List();
// Iterate over the array
for(int i = 0; i < n; i++)
{
// Push array elements
// alternately to the front
// and back
if ((i & 1) != 0)
ans.Insert(0, arr[i]);
else
ans.Add(arr[i]);
}
// If size of list is odd
if ((n & 1) != 0)
{
// Reverse the list
ans.Reverse();
}
// Print the elements
// of the array
foreach(int x in ans)
{
Console.Write(x + " ");
}
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
int n = 4;
int []arr = {1, 2, 3, 4};
generateArray(arr, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
4 2 1 3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。