原文:
给定一个大小为 n 的数组和一个值为 k ,我们需要围绕它右旋转数组。如何快速打印右旋转数组? 例:
input: array[] = {1, 3, 5, 7, 9}, k = 2.
output: 7 9 1 3 5
explanation:
after 1st rotation - {9, 1, 3, 5, 7}
after 2nd rotation - {7, 9, 1, 3, 5}
input: array[] = {1, 2, 3, 4, 5}, k = 4.
output: 2 3 4 5 1
进场:
-
我们将首先取 k 乘以 n (k = k % n)的 mod,因为在每 n 次旋转之后,数组将变得与初始数组相同。
-
现在,我们将数组从 i = 0 迭代到 i = n-1 并检查,
-
如果 i < k ,打印最右边的第 kth 个元素(a[n i -k])。否则,
-
在“k”元素后打印数组(a[i–k])。
-
下面是上述方法的实现。
c
// c implementation of right rotation
// of an array k number of times
#include
using namespace std;
// function to rightrotate array
void rightrotate(int a[], int n, int k)
{
// if rotation is greater
// than size of array
k = k % n;
for(int i = 0; i < n; i )
{
if(i < k)
{
// printing rightmost
// kth elements
cout << a[n i - k] << " ";
}
else
{
// prints array after
// 'k' elements
cout << (a[i - k]) << " ";
}
}
cout << "\n";
}
// driver code
int main()
{
int array[] = { 1, 2, 3, 4, 5 };
int n = sizeof(array) / sizeof(array[0]);
int k = 2;
rightrotate(array, n, k);
}
// this code is contributed by surendra_gangwar
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of right rotation
// of an array k number of times
import java.util.*;
import java.lang.*;
import java.io.*;
class array_rotation
{
// function to rightrotate array
static void rightrotate(int a[],
int n, int k)
{
// if rotation is greater
// than size of array
k=k%n;
for(int i = 0; i < n; i )
{
if(i
python 3
# python3 implementation of right rotation
# of an array k number of times
# function to rightrotate array
def rightrotate(a, n, k):
# if rotation is greater
# than size of array
k = k % n;
for i in range(0, n):
if(i < k):
# printing rightmost
# kth elements
print(a[n i - k], end = " ");
else:
# prints array after
# 'k' elements
print(a[i - k], end = " ");
print("\n");
# driver code
array = [ 1, 2, 3, 4, 5 ];
n = len(array);
k = 2;
rightrotate(array, n, k);
# this code is contributed by code_mech
c
// c# implementation of right rotation
// of an array k number of times
using system;
class gfg{
// function to rightrotate array
static void rightrotate(int []a,
int n, int k)
{
// if rotation is greater
// than size of array
k = k % n;
for(int i = 0; i < n; i )
{
if(i < k)
{
// printing rightmost
// kth elements
console.write(a[n i - k] " ");
}
else
{
// prints array after
// 'k' elements
console.write(a[i - k] " ");
}
}
console.writeline();
}
// driver code
public static void main(string []args)
{
int []array = { 1, 2, 3, 4, 5 };
int n = array.length;
int k = 2;
rightrotate(array, n, k);
}
}
// this code is contributed by rohit_ranjan
java 描述语言
// javascript implementation of right rotation
// of an array k number of times
// function to rightrotate array
function rightrotate(a, n, k)
{
// if rotation is greater
// than size of array
k = k % n;
for (let i = 0; i < n; i ) {
if (i < k) {
// printing rightmost
// kth elements
document.write(a[n i - k] " ");
}
else {
// prints array after
// 'k' elements
document.write((a[i - k]) " ");
}
}
document.write("
");
}
// driver code
let array = [1, 2, 3, 4, 5];
let n = array.length;
let k = 2;
rightrotate(array, n, k);
// this code is contributed by gfgking.
output:
4 5 1 2 3
时间复杂度:o(n)
t3】辅助空间 : o(1)
请参阅以下帖子了解阵列旋转的其他方法:
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处