1. 程式人生 > >[GeeksForGeeks] Cyclically rotate an array by one

[GeeksForGeeks] Cyclically rotate an array by one

integer amp set arr ast ace pick positive variable

Given an array of integers, write a program that cyclically rotates the array by one.

Example: given {1,2,3,4,5}, your program should rotate the array to {5,1,2,3,4}

Algorithm:

1. save the last element in a temp variable.

2. starting from the last element until the second element, copy its previous element to be their values.

3. copy temp to the first element.

O(n) runtime, O(1) space

 1 public void rotateByOne(int[] arr) {
 2     if(arr == null || arr.length == 0){
 3         return;
 4     }
 5     int temp = arr[arr.length - 1];
 6     for(int i = arr.length - 1; i > 0; i--){
 7         arr[i] = arr[i - 1];
 8     }
9 arr[0] = temp; 10 }

Follow up question: instead of rotating the given array by one position, rotate it by m positions, m should be controlled by

your program caller. if m is positive, it means rotating right, if m is negative, it means rotating left.

Solution 1. O(m * n) runtime

Simply call rotateByOne m times.

Solution 2. O(n) runtime, O(1) space

Algorithm: Acheive the rotation by swaping values only one time instead of m times for each location.

1. pick one element and save its value in a temp variable.

2. copy the new value from the source location to this destination location. Update destination location

to the source location; Repeat this process until there is only one element left that has not been correctly

updated. Set this element‘s value to temp.

 1 public class Solution {
 2     private void rightRotateByOne(int[] arr) {
 3         int temp = arr[arr.length - 1];
 4         for(int i = arr.length - 1; i > 0; i--){
 5             arr[i] = arr[i - 1];
 6         }
 7         arr[0] = temp;
 8     }
 9     private void leftRotateByOne(int[] arr) {
10         int temp = arr[0];
11         for(int i = 0; i < arr.length - 1; i++){
12             arr[i] = arr[i + 1];
13         }
14         arr[arr.length - 1] = temp;
15     }
16     private void rightRotateByM(int[] arr, int m) {
17         //O(m * n) runtime
18         /*for(int i = 0; i < m; i++){
19             rightRotateByOne(arr);            
20         }*/
21         //O(n) runtime
22         if(m % arr.length != 0) {
23             m %= arr.length;
24             int count = 0;
25             int currIdx = arr.length - 1;
26             int temp = arr[arr.length - 1];
27             while(count < arr.length - 1){                
28                 arr[currIdx] = arr[(currIdx - m + arr.length) % arr.length];
29                 currIdx = (currIdx - m + arr.length) % arr.length;
30                 count++;
31             }
32             arr[currIdx] = temp;
33         }
34     }
35     private void leftRotateByM(int[] arr, int m) {
36         //O(m * n) runtime
37         /*for(int i = 0; i < m; i++){
38             leftRotateByOne(arr);
39         }*/
40         //O(n) runtime
41         if(m % arr.length != 0) {
42             m %= arr.length;
43             int count = 0;
44             int currIdx = 0;
45             int temp = arr[0];
46             while(count < arr.length - 1){                
47                 arr[currIdx] = arr[(currIdx + m) % arr.length];
48                 currIdx = (currIdx + m) % arr.length;
49                 count++;
50             }
51             arr[currIdx] = temp;
52         }        
53     }
54     public void rotateByM(int[] arr, int m) {
55         if(arr == null || arr.length == 0 || m % arr.length == 0){
56             return;
57         }
58         if(m > 0) {
59             rightRotateByM(arr, m);            
60         }
61         if(m < 0) {
62             leftRotateByM(arr, Math.abs(m));
63         }
64     }
65 }

[GeeksForGeeks] Cyclically rotate an array by one