Java - Rotating array

后端 未结 14 1131

So the goal is to rotate the elements in an array right a times. As an example; if a==2, then array = {0,1,2,3,4} would become

14条回答
  •  执念已碎
    2020-11-27 20:01

    In ruby rotating an array can be possible in one line.

    def array_rotate(arr)
       i, j = arr.length - 1, 0
       arr[j],arr[i], i, j = arr[i], arr[j], i - 1, j + 1 while(j

提交回复
热议问题