posted 15 years ago
hi all, need some help!
I'm designing a method that can rotate array elements clockwise or anticlockwise some no. of times and return the result.
public int[] rotate(int[] array, int num_rotation, boolean clockwise)
Take for instance, int[] a = {1, 2, 3, 4, 5};
ANTICLOCKWISE
i.e last element become first, the rest push from L to R in an anticlockwise fasion
1st clockwise rotation => result: {5, 1, 2, 3, 4}
2nd clockwise rotation => result: {4, 5, 1, 2, 3}
and so on..
----------------------------------------------------------------------
CLOCKWISE
i.e first element become last, the rest push from R to L in an clockwise fashion
1st anti-clockwise rotation => result: {2, 3, 4, 5, 1}
2nd anti-clockwise rotation => result: {3, 4, 5, 1, 2}
and so on..
Can't figure the algorithm for rotation
Is there some better way of doing it.
Thanks in adv