Java - Rotating array

后端 未结 14 1072

So the goal is to rotate the elements in an array right a times. As an example; if a==2, then array = {0,1,2,3,4} would become

相关标签:
14条回答
  • 2020-11-27 19:56

    Java solution wrapped in a method:

    public static int[] rotate(final int[] array, final int rIndex) {
        if (array == null || array.length <= 1) {
            return new int[0];
        }
    
        final int[] result = new int[array.length];
        final int arrayLength = array.length;
    
        for (int i = 0; i < arrayLength; i++) {
            int nIndex = (i + rIndex) % arrayLength;
            result[nIndex] = array[i];
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-11-27 19:57

    Question : https://www.hackerrank.com/challenges/ctci-array-left-rotation
    Solution : This is how I tried arrayLeftRotation method with complexity o(n)

    • looping once from k index to (length-1 )
    • 2nd time for 0 to kth index

      public static int[] arrayLeftRotation(int[] a, int n, int k) {
      int[] resultArray = new int[n];
      int arrayIndex = 0;
      //first n-k indexes will be populated in this loop
      for(int i = k ; i resultArray[arrayIndex] = a[i];
      arrayIndex++;
      }
      // 2nd k indexes will be populated in this loop
      for(int j=arrayIndex ; j<(arrayIndex+k); j++){
      resultArray[j]=a[j-(n-k)];
      }
      return resultArray;
      }

    0 讨论(0)
  • 2020-11-27 19:57
    package com.array.orderstatistics;
    
    import java.util.Scanner;
    
    public class ArrayRotation {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int n = scan.nextInt();
            int r = scan.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scan.nextInt();
            }
            scan.close();
    
            if (r % n == 0) {
                printOriginalArray(a);
            } else {
                r = r % n;
                for (int i = 0; i < n; i++) {
                    b[i] = a[(i + r) < n ? (i + r) : ((i + r) - n)];
                    System.out.print(b[i] + " ");
                }
            }
        }
    
        private static void printOriginalArray(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 20:01

    For Left Rotate its very simple

    Take the difference between length of the array and number of position to shift.

    For Example

    int k = 2;
    int n = 5;
    
    int diff = n - k;
    
    int[] array = {1, 2, 3, 4, 5};
    int[] result = new int[array.length];
    System.arraycopy(array, 0, result, diff, k);
    System.arraycopy(array, k, result, 0, diff);
    

    // print the output

    0 讨论(0)
  • 2020-11-27 20:01

    In ruby rotating an array can be possible in one line.

    def array_rotate(arr)
       i, j = arr.length - 1, 0
       arr[j],arr[i], i, j = arr[i], arr[j], i - 1, j + 1 while(j<arr.length/2)
       puts "#{arr}"
    end
    
    0 讨论(0)
  • 2020-11-27 20:02

    Another way is copying with System.arraycopy.

        int[] temp = new int[array.length];
        System.arraycopy(array, 0, temp, a, array.length - a);
        System.arraycopy(array, array.length-a, temp, 0, a);
    
    0 讨论(0)
提交回复
热议问题