📌  相关文章
📜  在给定大小的组中反转数组

📅  最后修改于: 2022-05-13 01:57:52.396000             🧑  作者: Mango

在给定大小的组中反转数组

给定一个数组,反转由连续 k 个元素组成的每个子数组。

例子:

方法:考虑从数组开头开始的每个大小为k的子数组并将其反转。我们需要处理一些特殊情况。如果 k 不是 n 的倍数,其中 n 是数组的大小,对于最后一组,我们将剩下少于 k 个元素,我们需要反转所有剩余的元素。如果k = 1 ,则数组应保持不变。如果 k >= n,我们反转数组中存在的所有元素。

下图是上述方法的试运行:

下面是上述方法的实现:

C++
// C++ program to reverse every sub-array formed by
// consecutive k elements
#include 
using namespace std;
 
// Function to reverse every sub-array formed by
// consecutive k elements
void reverse(int arr[], int n, int k)
{
    for (int i = 0; i < n; i += k)
    {
        int left = i;
 
        // to handle case when k is not multiple of n
        int right = min(i + k - 1, n - 1);
 
        // reverse the sub-array [left, right]
        while (left < right)
            swap(arr[left++], arr[right--]);
 
    }
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int k = 3;
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    reverse(arr, n, k);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}


Java
// Java program to reverse every sub-array formed by
// consecutive k elements
class GFG {
     
    // Function to reverse every sub-array formed by
    // consecutive k elements
    static void reverse(int arr[], int n, int k)
    {
        for (int i = 0; i < n; i += k)
        {
            int left = i;
     
            // to handle case when k is not multiple
            // of n
            int right = Math.min(i + k - 1, n - 1);
            int temp;
             
            // reverse the sub-array [left, right]
            while (left < right)
            {
                temp=arr[left];
                arr[left]=arr[right];
                arr[right]=temp;
                left+=1;
                right-=1;
            }
        }
    }
     
    // Driver method
    public static void main(String[] args)
    {
         
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
        int k = 3;
     
        int n = arr.length;
     
        reverse(arr, n, k);
     
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python 3 program to reverse every
# sub-array formed by consecutive k
# elements
 
# Function to reverse every sub-array
# formed by consecutive k elements
def reverse(arr, n, k):
    i = 0
     
    while(i


C#
// C# program to reverse every sub-array
// formed by consecutive k elements
using System;
 
class GFG
{
 
// Function to reverse every sub-array
// formed by consecutive k elements
public static void reverse(int[] arr,
                           int n, int k)
{
    for (int i = 0; i < n; i += k)
    {
        int left = i;
 
        // to handle case when k is
        // not multiple of n
        int right = Math.Min(i + k - 1, n - 1);
        int temp;
 
        // reverse the sub-array [left, right]
        while (left < right)
        {
            temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left += 1;
            right -= 1;
        }
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = new int[] {1, 2, 3, 4,
                           5, 6, 7, 8};
    int k = 3;
 
    int n = arr.Length;
 
    reverse(arr, n, k);
 
    for (int i = 0; i < n; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
}
 
// This code is contributed
// by Shrikant13


PHP


Javascript


输出:

3 2 1 6 5 4 8 7

上述解决方案的时间复杂度为 O(n)。
程序使用的辅助空间为 O(1)。