快速查找数组的多个左旋转 |设置 1
给定一个大小为 n 的数组和多个值,我们需要围绕该数组左旋。如何快速找到多个左旋转?
例子:
Input : arr[] = {1, 3, 5, 7, 9}
k1 = 1
k2 = 3
k3 = 4
k4 = 6
Output : 3 5 7 9 1
7 9 1 3 5
9 1 3 5 7
3 5 7 9 1
Input : arr[] = {1, 3, 5, 7, 9}
k1 = 14
Output : 9 1 3 5 7
简单方法:我们已经讨论了以下帖子中给出的不同方法。
- 数组的左旋转(简单和杂耍算法)。
- 数组旋转的块交换算法
- 数组旋转的反转算法
上述方法中最好的方法需要 O(n) 时间和 O(1) 额外空间。
有效的方法:
当需要单次旋转时,上述方法效果很好。这些方法还修改了原始数组。为了处理数组旋转的多个查询,我们使用大小为 2n 的临时数组并快速处理旋转。
第 1 步:在 temp[0..2n-1] 数组中复制整个数组两次。
步骤 2:在 temp[] 中经过 k 次旋转后数组的起始位置将是 k % n。我们做
第 3 步:将 temp[] 数组从 k % n 打印到 k % n + n。
C++
// CPP implementation of left rotation of
// an array K number of times
#include
using namespace std;
// Fills temp[] with two copies of arr[]
void preprocess(int arr[], int n, int temp[])
{
// Store arr[] elements at i and i + n
for (int i = 0; i
Java
// Java implementation of left rotation of
// an array K number of times
class LeftRotate
{
// Fills temp[] with two copies of arr[]
static void preprocess(int arr[], int n,
int temp[])
{
// Store arr[] elements at i and i + n
for (int i = 0; i
Python3
# Python3 implementation of left rotation
# of an array K number of times
# Fills temp with two copies of arr
def preprocess(arr, n):
temp = [None] * (2 * n)
# Store arr elements at i and i + n
for i in range(n):
temp[i] = temp[i + n] = arr[i]
return temp
# Function to left rotate an array k times
def leftRotate(arr, n, k, temp):
# Starting position of array after k
# rotations in temp will be k % n
start = k % n
# Print array after k rotations
for i in range(start, start + n):
print(temp[i], end = " ")
print("")
# Driver program
arr = [1, 3, 5, 7, 9]
n = len(arr)
temp = preprocess(arr, n)
k = 2
leftRotate(arr, n, k, temp)
k = 3
leftRotate(arr, n, k, temp)
k = 4
leftRotate(arr, n, k, temp)
# This code is contributed by Sanghamitra Mishra
C#
// C# implementation of left rotation of
// an array K number of times
using System;
class LeftRotate
{
// Fills temp[] with two copies of arr[]
static void preprocess(int []arr, int n,
int[] temp)
{
// Store arr[] elements at i and i + n
for (int i = 0; i
PHP
Javascript
C++
// CPP implementation of left rotation of
// an array K number of times
#include
using namespace std;
// Function to left rotate an array k times
void leftRotate(int arr[], int n, int k)
{
// Print array after k rotations
for (int i = k; i < k + n; i++)
cout << arr[i%n] << " ";
}
// Driver program
int main()
{
int arr[] = {1, 3, 5, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
leftRotate(arr, n, k);
cout << endl;
k = 3;
leftRotate(arr, n, k);
cout << endl;
k = 4;
leftRotate(arr, n, k);
cout << endl;
return 0;
}
Java
// Java implementation of
// left rotation of an
// array K number of times
import java.io.*;
class GFG
{
// Function to left rotate
// an array k times
static void leftRotate(int arr[],
int n, int k)
{
// Print array after
// k rotations
for (int i = k; i < k + n; i++)
System.out.print(arr[i % n] + " ");
}
// Driver Code
public static void main (String[] args)
{
int arr[] = {1, 3, 5, 7, 9};
int n = arr.length;
int k = 2;
leftRotate(arr, n, k);
System.out.println();
k = 3;
leftRotate(arr, n, k);
System.out.println();
k = 4;
leftRotate(arr, n, k);
System.out.println();
}
}
// This code is contributed by ajit
Python 3
# Python3 implementation of
# left rotation of an array
# K number of times
# Function to left rotate
# an array k times
def leftRotate(arr, n, k):
# Print array
# after k rotations
for i in range(k, k + n):
print(str(arr[i % n]),
end = " ")
# Driver Code
arr = [1, 3, 5, 7, 9]
n = len(arr)
k = 2;
leftRotate(arr, n, k)
print()
k = 3;
leftRotate(arr, n, k)
print()
k = 4
leftRotate(arr, n, k)
print()
# This code is contributed
# by ChitraNayal
C#
// C# implementation of
// left rotation of an
// array K number of times
using System;
class GFG
{
// Function to left rotate
// an array k times
static void leftRotate(int []arr,
int n, int k)
{
// Print array after
// k rotations
for (int i = k; i < k + n; i++)
Console.Write(arr[i % n] + " ");
}
// Driver Code
static public void Main ()
{
int []arr = {1, 3, 5, 7, 9};
int n = arr.Length;
int k = 2;
leftRotate(arr, n, k);
Console.WriteLine();
k = 3;
leftRotate(arr, n, k);
Console.WriteLine();
k = 4;
leftRotate(arr, n, k);
Console.WriteLine();
}
}
// This code is contributed
// by akt_mit
PHP
Javascript
输出:
5 7 9 1 3
7 9 1 3 5
9 1 3 5 7
请注意,查找旋转起始地址的任务需要O(1) 时间。它正在打印需要 O(n) 时间的元素。
空间优化方法:上述方法需要额外的空间。下面给出的是一个空间优化的解决方案。感谢frenzy77提出这种方法。
C++
// CPP implementation of left rotation of
// an array K number of times
#include
using namespace std;
// Function to left rotate an array k times
void leftRotate(int arr[], int n, int k)
{
// Print array after k rotations
for (int i = k; i < k + n; i++)
cout << arr[i%n] << " ";
}
// Driver program
int main()
{
int arr[] = {1, 3, 5, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
leftRotate(arr, n, k);
cout << endl;
k = 3;
leftRotate(arr, n, k);
cout << endl;
k = 4;
leftRotate(arr, n, k);
cout << endl;
return 0;
}
Java
// Java implementation of
// left rotation of an
// array K number of times
import java.io.*;
class GFG
{
// Function to left rotate
// an array k times
static void leftRotate(int arr[],
int n, int k)
{
// Print array after
// k rotations
for (int i = k; i < k + n; i++)
System.out.print(arr[i % n] + " ");
}
// Driver Code
public static void main (String[] args)
{
int arr[] = {1, 3, 5, 7, 9};
int n = arr.length;
int k = 2;
leftRotate(arr, n, k);
System.out.println();
k = 3;
leftRotate(arr, n, k);
System.out.println();
k = 4;
leftRotate(arr, n, k);
System.out.println();
}
}
// This code is contributed by ajit
Python3
# Python3 implementation of
# left rotation of an array
# K number of times
# Function to left rotate
# an array k times
def leftRotate(arr, n, k):
# Print array
# after k rotations
for i in range(k, k + n):
print(str(arr[i % n]),
end = " ")
# Driver Code
arr = [1, 3, 5, 7, 9]
n = len(arr)
k = 2;
leftRotate(arr, n, k)
print()
k = 3;
leftRotate(arr, n, k)
print()
k = 4
leftRotate(arr, n, k)
print()
# This code is contributed
# by ChitraNayal
C#
// C# implementation of
// left rotation of an
// array K number of times
using System;
class GFG
{
// Function to left rotate
// an array k times
static void leftRotate(int []arr,
int n, int k)
{
// Print array after
// k rotations
for (int i = k; i < k + n; i++)
Console.Write(arr[i % n] + " ");
}
// Driver Code
static public void Main ()
{
int []arr = {1, 3, 5, 7, 9};
int n = arr.Length;
int k = 2;
leftRotate(arr, n, k);
Console.WriteLine();
k = 3;
leftRotate(arr, n, k);
Console.WriteLine();
k = 4;
leftRotate(arr, n, k);
Console.WriteLine();
}
}
// This code is contributed
// by akt_mit
PHP
Javascript
输出:
5 7 9 1 3
7 9 1 3 5
9 1 3 5 7