时间复杂度o(n2)
1234abcd -> 4321abcd -> 4321dcba -> abcd1234
1 int change(int *a, int k) 2 { 3 int i, tmp; 4 k %= LEN; 5 while (k-- > 0) { 6 tmp = a[LEN - 1]; 7 for (i = LEN - 1; i > 0; i--) { 8 a[i] = a[i - 1]; 9 } 10 a[0] = tmp; 11 } 12 13 return 0; 14 }
时间复杂度o(n)
1 int reverse(int *a, int start, int end) 2 { 3 int tmp; 4 for (; start < end; start++, end--) { 5 tmp = a[end]; 6 a[end] = a[start]; 7 a[start] = tmp; 8 } 9 return 0; 10 } 11 12 int change1(int *a, int k) 13 { 14 k %= LEN; 15 reverse(a, 0, LEN - k - 1); 16 reverse(a, LEN - k, LEN - 1); 17 reverse(a, 0, LEN - 1); 18 return 0; 19 }