1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* * A = array yang akan dirotasi * k = besar rotasi ke kanan */ public static int[] cyclicRotation(int[] A, int k) { if(A.length<=1) return A; int[] temp = new int[A.length]; int rot = (k/A.length) > 0 ? k%A.length: k; for(int i=0; i<A.length; i++) { if(A.length - i > rot) temp[rot+i] = A[i]; else temp[rot-(A.length-i)] = A[i]; } return temp; } |
Hasil Uji dari Codiliti :
No comments:
Post a Comment