[GeeksForGeeks] Cyclically rotate an array by one

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GeeksForGeeks] Cyclically rotate an array by one相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

geeksforgeeks@ Equal to product (Binary Search)

geeksforgeeks@ Maximum Index (Dynamic Programming)

c_cpp 键盘打字 - GeeksforGeeks

geeksforgeeks@ Minimum sum partition (Dynamic Programming)

[GeeksForGeeks] Perfect Binary Tree

[GeeksForGeeks] Friends Pairing Problem