原文:

给定一个 arr[],任务是通过在打印每个元素后翻转整个数组来打印从第一个到最后一个索引遍历给定数组形成的数组。

示例:

输入: arr = {0,1,2,3,4,5} 输出:0 4 2 4 0 解释:在第一次迭代时,索引 0 - > 0 上的元素被打印,然后整个数组被翻转:{ 0 ,1,2,3,4,5} - > {5,4,3,2,1, 0} 在第二次迭代中,索引 1 - > 4 上的元素被打印,然后整个数组被翻转::{5, 4 ,3,2,1,0} - > {0,1,2,3,4,5} 在第三次迭代中,索引 2 - > 2 上的元素被打印,然后整个数组被翻转:{0,1, 2 ,3,4,5} 0} 在第二次迭代中,索引 3 - > 2 上的元素被打印,然后整个数组被翻转::{5,4,3, 2 ,1,0} - > {0,1,2,3,4,5} 在第二次迭代中,索引 4 - > 4 上的元素被打印,然后整个数组被翻转:{0,1,2,3, 4 ,5 0} 在第二次迭代中,索引 5 上的元素- > 0 被打印,然后整个数组被翻转::{5,4,3,2,1,0}->

输入: arr = {0,1,2,3,4} 输出: 0 3 2 1 4

方法:给定的问题可以通过使用来解决。思路是从第一个索引开始从左到右迭代数组,从第二个最后一个索引开始从右到左迭代数组。

可以遵循以下步骤来解决问题:

  • 使用指针一从左到右迭代数组,使用指针二从右到左遍历数组
  • 同时打印两个指针指向的元素,并将指针 1 增加 2,将指针 2 减少 2

下面是上述方法的实现:

c

// c   code for the above approach
#include 
using namespace std;
// function to print array elements at
// every index by flipping whole array
// after printing every element
void printflip(vector arr)
{
    // initialize length of the array
    int n = arr.size();
    // initialize both the pointers
    int p1 = 0, p2 = n - 2;
    // iterate until both pointers
    // are not out of bounds
    while (p1 < n || p2 >= 0) {
        // print the elements
        cout << arr[p1] << " ";
        if (p2 > 0)
            cout << arr[p2] << " ";
        // increment p1 by 2
        p1  = 2;
        // decrement p2 by 2
        p2 -= 2;
    }
}
// driver code
int main()
{
    // initialize the array
    vector arr = { 0, 1, 2, 3, 4 };
    // call the function
    // and print the array
    printflip(arr);
    return 0;
}
// this code is contributed by potta lokesh.

java 语言(一种计算机语言,尤用于创建网站)

// java implementation for the above approach
import java.io.*;
import java.util.*;
class gfg {
    // function to print array elements at
    // every index by flipping whole array
    // after printing every element
    public static void printflip(int[] arr)
    {
        // initialize length of the array
        int n = arr.length;
        // initialize both the pointers
        int p1 = 0, p2 = n - 2;
        // iterate until both pointers
        // are not out of bounds
        while (p1 < n || p2 >= 0) {
            // print the elements
            system.out.print(arr[p1]   " ");
            if (p2 > 0)
                system.out.print(arr[p2]   " ");
            // increment p1 by 2
            p1  = 2;
            // decrement p2 by 2
            p2 -= 2;
        }
    }
    // driver code
    public static void main(string[] args)
    {
        // initialize the array
        int[] arr = { 0, 1, 2, 3, 4 };
        // call the function
        // and print the array
        printflip(arr);
    }
}

python 3

# python code for the above approach
# function to print array elements at
# every index by flipping whole array
# after printing every element
def printflip(arr):
    # initialize length of the array
    n = len(arr);
    # initialize both the pointers
    p1 = 0
    p2 = n - 2;
    # iterate until both pointers
    # are not out of bounds
    while (p1 < n or p2 >= 0):
        # print the elements
        print(arr[p1], end=" ");
        if (p2 > 0):
            print(arr[p2], end=" ");
        # increment p1 by 2
        p1  = 2;
        # decrement p2 by 2
        p2 -= 2;
# driver code
# initialize the array
arr = [ 0, 1, 2, 3, 4 ];
# call the function
# and print the array
printflip(arr);
# this code is contributed by gfgking.

c

// c# implementation for the above approach
using system;
class gfg {
    // function to print array elements at
    // every index by flipping whole array
    // after printing every element
    static void printflip(int []arr)
    {
        // initialize length of the array
        int n = arr.length;
        // initialize both the pointers
        int p1 = 0, p2 = n - 2;
        // iterate until both pointers
        // are not out of bounds
        while (p1 < n || p2 >= 0) {
            // print the elements
            console.write(arr[p1]   " ");
            if (p2 > 0)
                console.write(arr[p2]   " ");
            // increment p1 by 2
            p1  = 2;
            // decrement p2 by 2
            p2 -= 2;
        }
    }
    // driver code
    public static void main()
    {
        // initialize the array
        int []arr = { 0, 1, 2, 3, 4 };
        // call the function
        // and print the array
        printflip(arr);
    }
}
// this code is contributed by samim hossain mondal.

java 描述语言


output

0 3 2 1 4 

时间复杂度:o(n) t3】辅助空间: o(1)