Rotate Array
Source code Read on Github
1 public class Solution {
2 public void rotate(int[] nums, int k) {
3
4 if(k == 0) return;
5
6
7 int c = 0;
8
9 for(int j = 0; j < k; j++) {
10 int i = j;
11 int t = nums[i];
12
13
14 for (;;) {
15
16 i = (i + k) % nums.length;
17
18 int nt = nums[i];
19
20 nums[i] = t;
21
22 t = nt;
23
24 c++;
25
26 if (i == j) break;
27 }
28
29 if(c == nums.length) break;
30 }
31
32 }
33 }