public class rotate { public static void rotate_int_array(int []array , int amount){ int array_size = array.length ; if(amount % array_size ==0) return; if(amount < 0){ amount = (-1) * amount; int temp=0; for(int i =0 ; i < amount ; i++){ temp=array[array.length-1]; for(int shift=array.length-1; shift >0 ; shift--){ array[shift] = array[shift-1]; } array[0] = temp; } } else{ if(amount > array_size) amount = amount % array_size; int temp=0; for(int i =0 ; i < amount ; i++){ temp=array[0]; for(int shift=0; shift < array.length -1 ; shift++){ array[shift] = array[shift+1]; } array[array.length -1] = temp; } } } }
Monday, April 11, 2011
rotate a int array by specific amount java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment