📜  右旋 K 次后打印数组,其中 K 可以很大或为负

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

向右旋转 K 次后打印数组,其中 K 可以很大或为负

给定一个大小为N的数组arr[]和一个值K ( -10^5) ,任务是打印向右旋转K次的数组。

例子:

Naive Approach:解决此问题的蛮力方法是使用临时数组将数组旋转 K 或 -K 次。

时间复杂度: O(N)
辅助空间: O(1)

有效的方法:给定的问题可以通过将问题分解为以下部分来解决:

  1. 使用以下步骤在 [0, N) 范围内四舍五入 K 的值:
    • 如果K是负数,先改成正数,求与N的模数,再改成负数
    • 如果 K 是正数,只需找到与 N 的模数
  2. 处理 K 为负数的情况。如果 K 为负数,则意味着我们需要将数组向左旋转 K 次,或者向右旋转 -K 次。
  3. 接下来我们可以通过反转子数组来简单地将数组旋转 K 次。可以按照以下步骤解决问题:
    • 将所有数组元素从 1 反转为 N -1
    • 将数组元素从 1 反转为 K – 1
    • 将数组元素从 K 反转为 N -1
C++
// C++ implementation for the above approach
 
#include 
using namespace std;
 
// Function to rotate the array
// to the right, K times
void RightRotate(vector& nums, int K)
{
    int n = nums.size();
 
    // Case when K > N or K < -N
    K = K < 0 ? ((K * -1) % n) * -1 : K % n;
 
    // Case when K is negative
    K = K < 0 ? (n - (K * -1)) : K;
 
    // Reverse all the array elements
    reverse(nums.begin(), nums.end());
 
    // Reverse the first k elements
    reverse(nums.begin(), nums.begin() + K);
 
    // Reverse the elements from K
    // till the end of the array
    reverse(nums.begin() + K, nums.end());
}
 
// Driver code
int main()
{
 
    // Initialize the array
    vector Array = { 1, 2, 3, 4, 5 };
 
    // Find the size of the array
    int N = Array.size();
 
    // Initialize K
    int K = -2;
 
    // Call the function and
    // print the answer
    RightRotate(Array, K);
 
    // Print the array after rotation
    for (int i = 0; i < N; i++) {
 
        cout << Array[i] << " ";
    }
 
    cout << endl;
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
 
class GFG{
 
    // Initialize the array
   static int[] Array = { 1, 2, 3, 4, 5 };
    
   static void reverse( int start, int end) {
 
       // Temporary variable to store character
       int temp;
       while (start <= end)
       {
          
           // Swapping the first and last character
           temp = Array[start];
           Array[start] = Array[end];
           Array[end] = temp;
           start++;
           end--;
       }
   }
   
// Function to rotate the array
// to the right, K times
static void RightRotate( int K)
{
    int n = Array.length;
 
    // Case when K > N or K < -N
    K = K < 0 ? ((K * -1) % n) * -1 : K % n;
 
    // Case when K is negative
    K = K < 0 ? (n - (K * -1)) : K;
 
    // Reverse all the array elements
    reverse(0, n-1);
 
    // Reverse the first k elements
    reverse(0, n - K);
 
    // Reverse the elements from K
    // till the end of the array
    reverse( K, n-1);
}
 
// Driver code
public static void main(String[] args)
{
 
 
    // Find the size of the array
    int N = Array.length;
 
    // Initialize K
    int K = -2;
 
    // Call the function and
    // print the answer
    RightRotate(K);
 
    // Print the array after rotation
    for (int i = 0; i < N; i++) {
 
        System.out.print(Array[i]+ " ");
    }
 
    System.out.println();
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python code for the above approach
 
# Function to rotate the array
# to the right, K times
def RightRotate(nums, K) :
    n = len(nums)
 
    # Case when K > N or K < -N
    K = ((K * -1) % n) * -1 if K < 0 else K % n;
 
    # Case when K is negative
    K = (n - (K * -1)) if K < 0 else K;
 
    # Reverse all the array elements
    nums.reverse();
 
    # Reverse the first k elements
    p1 = nums[0:K]
    p1.reverse();
 
    # Reverse the elements from K
    # till the end of the array
    p2 = nums[K:]
    p2.reverse();
    arr = p1 + p2
 
    return arr;
 
# Driver code
 
# Initialize the array
Array = [1, 2, 3, 4, 5];
 
# Find the size of the array
N = len(Array)
 
# Initialize K
K = -2;
 
# Call the function and
# print the answer
Array = RightRotate(Array, K);
 
# Print the array after rotation
for i in Array:
    print(i, end=" ")
 
# This code is contributed by Saurabh jaiswal


C#
// C# implementation for the above approach
using System;
public class GFG {
 
  // Initialize the array
  static int[] Array = { 1, 2, 3, 4, 5 };
  static void reverse(int start, int end)
  {
 
    // Temporary variable to store character
    int temp;
    while (start <= end) {
 
      // Swapping the first and last character
      temp = Array[start];
      Array[start] = Array[end];
      Array[end] = temp;
      start++;
      end--;
    }
  }
 
  // Function to rotate the array
  // to the right, K times
  static void RightRotate(int K) {
    int n = Array.Length;
 
    // Case when K > N or K < -N
    K = K < 0 ? ((K * -1) % n) * -1 : K % n;
 
    // Case when K is negative
    K = K < 0 ? (n - (K * -1)) : K;
 
    // Reverse all the array elements
    reverse(0, n - 1);
 
    // Reverse the first k elements
    reverse(0, n - K);
 
    // Reverse the elements from K
    // till the end of the array
    reverse(K, n - 1);
  }
 
  // Driver code
  public static void Main(String[] args) {
 
    // Find the size of the array
    int N = Array.Length;
 
    // Initialize K
    int K = -2;
 
    // Call the function and
    // print the answer
    RightRotate(K);
 
    // Print the array after rotation
    for (int i = 0; i < N; i++) {
 
      Console.Write(Array[i] + " ");
    }
 
    Console.WriteLine();
  }
}
 
// This code is contributed by Rajput-Ji


Javascript



输出
3 4 5 1 2 

时间复杂度: O(N)
辅助空间: O(1)