码迷,mamicode.com
首页 > 其他好文 > 详细

[GeeksForGeeks] Cyclically rotate an array by one

时间:2017-08-12 10:19:59      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:bsp   runtime   geeks   follow   private   solution   update   var   cal   

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

标签:bsp   runtime   geeks   follow   private   solution   update   var   cal   

原文地址:http://www.cnblogs.com/lz87/p/7348678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!