原文:
给定大小为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
我们在下面的文章中讨论了一个pg电子试玩链接的解决方案。
上面讨论的pg电子试玩链接的解决方案需要额外的空间。 在本文中,我们讨论了不需要额外空间的优化pg电子试玩链接的解决方案。
c
// cpp implementation of left rotation of
// an array k number of times
#include
using namespace std;
// function to leftrotate array multiple times
void leftrotate(int arr[], int n, int k)
{
/* to get the starting point of rotated array */
int mod = k % n;
// prints the rotated array from start position
for (int i = 0; i < n; i )
cout << (arr[(mod i) % n]) << " ";
cout << "\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);
k = 3;
leftrotate(arr, n, k);
k = 4;
leftrotate(arr, n, k);
return 0;
}
java
// java implementation of left rotation
// of an array k number of times
import java.util.*;
import java.lang.*;
import java.io.*;
class arr_rot
{
// function to leftrotate array multiple
// times
static void leftrotate(int arr[], int n,
int k)
{
/* to get the starting point of
rotated array */
int mod = k % n;
// prints the rotated array from
// start position
for(int i = 0; i < n; i)
system.out.print(arr[(i mod) % n]
" ");
system.out.println();
}
// driver program
public static void main (string[] args)
{
int arr[] = { 1, 3, 5, 7, 9 };
int n = arr.length;
int k = 2;
leftrotate(arr, n, k);
k = 3;
leftrotate(arr, n, k);
k = 4;
leftrotate(arr, n, k);
}
}
// this code is contributed by sanjal
python
# python implementation of left rotation of
# an array k number of times
# function to leftrotate array multiple times
def leftrotate(arr, n, k):
# to get the starting point of rotated array
mod = k % n
s = ""
# prints the rotated array from start position
for i in range(n):
print str(arr[(mod i) % n]),
print
return
# driver program
arr = [ 1, 3, 5, 7, 9 ]
n = len(arr)
k = 2
leftrotate(arr, n, k)
k = 3
leftrotate(arr, n, k)
k = 4
leftrotate(arr, n, k)
#this code is contributed by sachin bisht
c#
// c# implementation of left
// rotation of an array k
// number of times
using system;
class gfg
{
// function to leftrotate
// array multiple times
static void leftrotate(int []arr,
int n, int k)
{
// to get the starting
// point of rotated array
int mod = k % n;
// prints the rotated array
// from start position
for(int i = 0; i < n; i)
console.write(arr[(i mod) %
n] " ");
console.writeline();
}
// driver code
static public void main ()
{
int []arr = {1, 3, 5, 7, 9};
int n = arr.length;
int k = 2;
leftrotate(arr, n, k);
k = 3;
leftrotate(arr, n, k);
k = 4;
leftrotate(arr, n, k);
}
}
// this code is contributed by m_kit
php
输出:
5 7 9 1 3
7 9 1 3 5
9 1 3 5 7
方法二:
在下面的实现中,我们将使用标准模板库(stl),它将使pg电子试玩链接的解决方案更加优化和易于实现。
c
// c implementation for print left rotation of any array k
// times
#include
#include
using namespace std;
// function for the k times left rotation
void leftrotate(int arr[], int k, int n)
{
// stl function rotates takes three parameters - the
// beginning,the position by which it should be rotated
// ,the end address of the array
// the below function will be rotating the array left
// in linear time (k%arraysize) times
rotate(arr, arr (k % n), arr n);
// print the rotated array from start position
for (int i = 0; i < n; i )
cout << arr[i] << " ";
cout << "\n";
}
// driver program
int main()
{
int arr[] = { 1, 3, 5, 7, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
// function call
leftrotate(arr, k, n);
return 0;
}
java
// java implementation for print left
// rotation of any array k times
import java.io.*;
import java.util.*;
class gfg{
// function for the k times left rotation
static void leftrotate(integer arr[], int k,
int n)
{
// in collection class rotate function
// takes two parameters - the name of
// array and the position by which it
// should be rotated
// the below function will be rotating
// the array left in linear time
// collections.rotate()rotate the
// array from right hence n-k
collections.rotate(arrays.aslist(arr), n - k);
// print the rotated array from start position
for(int i = 0; i < n; i )
system.out.print(arr[i] " ");
}
// driver code
public static void main(string[] args)
{
integer arr[] = { 1, 3, 5, 7, 9 };
int n = arr.length;
int k = 2;
// function call
leftrotate(arr, k, n);
}
}
// this code is contributed by chahattekwani71
输出:
5 7 9 1 3
注意:旋转后,数组本身会更新。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处