circular left shift of an array by n positions in java

前端 未结 12 2283
醉梦人生
醉梦人生 2020-12-31 20:16

I am trying to do the circular left shift of an array by n positions using only a single 1D array. I can do it in two arrays, but I haven\'t figured out how to do it using o

相关标签:
12条回答
  • 2020-12-31 20:53

    Another alternative would be to wrap up your own structure, which includes the array and the index of virtual zero.

    0 讨论(0)
  • 2020-12-31 20:55

    Maybe an old post.. but here you are my solution (where A obviously is the array and K the number of positions).

    public int[] solution(int[] A, int K){
        int[] result = new int[A.length];
    
        for (int i = 0; i<A.length; i++){
            result[(i+K)%A.length] = A[i];
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-31 20:55

    How about this?

        // Left shift the array in O(n) with O(1) space.
    
    public static void leftShift(int[] array, int n) {
        int temp;
        int len = array.length;
        for (int i = 0; i < n; i++) {
            temp = array[len - n + i];
            array[len - n + i] = array[i];
            array[i] = array[n + i];
            array[n + i] = temp;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 20:58

    Java 8 version:

    public class Sample {
       public static void main(String[] args) {
         int[] answer = solution(new int[] {1,2,3,4}, 2);
         Arrays.stream(answer).forEach(System.out::print);
       }
    
       public static int[] solution(int[] A, int K) {
         List<Integer> numbers = 
         IntStream.of(A).boxed().collect(Collectors.toList());
         Collections.rotate(numbers, K);
         return numbers.stream().mapToInt(n -> n).toArray();
      }
    }
    
    0 讨论(0)
  • 2020-12-31 21:05

    Checkout this github link:

    https://github.com/techpanja/interviewproblems/blob/master/src/arrays/circularshiftintarray/CircularShiftArray.java

    circularShiftToLeftInPlace

    0 讨论(0)
  • 2020-12-31 21:05

    Below I have implemented a sample solution to left shift or right shift the array by n element.

    class RotateArrayByN {
    
        public void leftRotate(int arr[], int d, int n)
        {
            for (int i = 0; i < d; i++)
                leftRotatebyOne(arr, n);
        }
    
        public void rightRotate(int[] arr,int d, int n){
            for(int i=0;i<d;i++)
                rightRotatebyOne(arr,n);
        }
    
        public void leftRotatebyOne(int arr[], int n)
        {
            int i, temp;
            temp = arr[0];
            for (i = 0; i < n - 1; i++)
                arr[i] = arr[i + 1];
            arr[i] = temp;
        }
    
        public void rightRotatebyOne(int[] arr,int n){
    
            int temp=arr[n-1];
            for (int i=n-1;i>0;i--) {
                arr[i] = arr[i - 1];
            }
            arr[0]=temp;
    
        }
    
      public void printArray(int arr[], int n)
        {
            for (int i = 0; i < n; i++)
                System.out.print(arr[i] + " ");
    
            System.out.println();
        }
    
    
        public static void main(String[] args)
        {
            RotateArrayByN rotate = new RotateArrayByN();
            int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
            System.out.println("Left Rotate");
            rotate.leftRotate(arr, 2, 7);
            rotate.printArray(arr, 7);
    
           //System.out.println("Right Rotate");
            //rotate.rightRotate(arr,2,7);
           // rotate.printArray(arr,7);
        }
    } 
    

    I have commented out the right shift.

    0 讨论(0)
提交回复
热议问题