2
2
3
3
public abstract class RotateArray189 {
4
4
5
- /**
6
- * Rotate an array of n elements to the right by k steps.
5
+ /**
6
+ * Rotate an array of n elements to the right by k steps.
7
+ * <p/>
8
+ * For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
9
+ */
7
10
8
- For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
9
- */
10
-
11
- public abstract void rotate (int [] nums , int k );
11
+ public abstract void rotate (int [] nums , int k );
12
12
13
- static class Solution1 extends RotateArray189 {
13
+ static class Solution1 extends RotateArray189 {
14
14
15
- public void swap (int [] a , int i , int j )
16
- {
17
- int temp = a [i ];
18
- a [i ] = a [j ];
19
- a [j ] = temp ;
20
- }
15
+ public void swap (int [] a , int i , int j ) {
16
+ int temp = a [i ];
17
+ a [i ] = a [j ];
18
+ a [j ] = temp ;
19
+ }
21
20
22
- /**
23
- * for example, array:[1,2,3,4,5,6], step=2
24
- * the steps will be:
25
- * [1,6,3,4,5,2], idx=1, temp=5
26
- * [1,6,3,2.5.4], idx=3, temp=5
27
- * [1,6,3,2,5,4], idx=(3+2)%6=5==temp, so temp=temp-1=4, idx=4, no element moved in this iteration
28
- * [5,6,3,2,1,4], idx=0, temp=4
29
- * [5,6,1,2,3,4], idx=2, temp=4, iteration exit.
30
- */
31
- public void rotate (int [] a , int k )
32
- {
33
- int N = a .length ;
34
- k = k %N ;
35
- int idx = N -1 ;
36
- int temp = N -1 ;
37
- /**
38
- * every step will swap two element, within which one element gets the final position.
39
- * So in total it only needs N-1 step.
40
- */
41
- for (int s = 0 ; s < N -1 ; s ++)
42
- {
43
- idx = (idx +k )%N ;
44
- if (temp == idx )
45
- {
46
- temp = temp -1 ;
47
- idx = temp ;
48
- continue ;
49
- }
50
- /*idx is now the final position of element at position temp*/
51
- swap (a ,temp ,idx );
52
- }
53
- }
54
- }
55
-
56
- static class Solution2 extends RotateArray189 {
21
+ /**
22
+ * for example, array:[1,2,3,4,5,6], step=2
23
+ * the steps will be:
24
+ * [1,6,3,4,5,2], idx=1, temp=5
25
+ * [1,6,3,2.5.4], idx=3, temp=5
26
+ * [1,6,3,2,5,4], idx=(3+2)%6=5==temp, so temp=temp-1=4, idx=4, no element moved in this iteration
27
+ * [5,6,3,2,1,4], idx=0, temp=4
28
+ * [5,6,1,2,3,4], idx=2, temp=4, iteration exit.
29
+ */
30
+ public void rotate (int [] a , int k ) {
31
+ int N = a .length ;
32
+ k = k % N ;
33
+ int idx = N - 1 ;
34
+ int temp = N - 1 ;
35
+ /**
36
+ * every step will swap two element, within which one element gets the final position.
37
+ * So in total it only needs N-1 step.
38
+ */
39
+ for (int s = 0 ; s < N - 1 ; s ++) {
40
+ idx = (idx + k ) % N ;
41
+ if (temp == idx ) {
42
+ temp = temp - 1 ;
43
+ idx = temp ;
44
+ continue ;
45
+ }
46
+ /*idx is now the final position of element at position temp*/
47
+ swap (a , temp , idx );
48
+ }
49
+ }
50
+ }
57
51
58
- /**
59
- * example [1,2,3,4,5,6,7], step=3
60
- *
61
- * rotate the [1,2,3,4] firstly as [4,3,2,1,5,6,7]
62
- * rotate the [5,6,7] secondly as [4,3,2,1,7,6,5]
63
- * rotate the whole array as [5,6,7,1,2,3,4]
64
- */
65
- @ Override
66
- public void rotate (int [] nums , int k ) {
67
- k =k %nums .length ;
68
- rotate (nums ,0 ,nums .length -k -1 );
69
- rotate (nums ,nums .length -k ,nums .length -1 );
70
- rotate (nums ,0 ,nums .length -1 );
71
- }
72
-
73
- private void rotate (int [] nums , int start , int end ){
74
- while (start <end ){
75
- nums [start ] ^= nums [end ];
76
- nums [end ] ^= nums [start ];
77
- nums [start ] ^= nums [end ];
78
- start ++;
79
- end --;
80
- }
81
- }
82
-
83
- }
52
+ static class Solution2 extends RotateArray189 {
53
+
54
+ /**
55
+ * example [1,2,3,4,5,6,7], step=3
56
+ * <p/>
57
+ * rotate the [1,2,3,4] firstly as [4,3,2,1,5,6,7]
58
+ * rotate the [5,6,7] secondly as [4,3,2,1,7,6,5]
59
+ * rotate the whole array as [5,6,7,1,2,3,4]
60
+ */
61
+ @ Override
62
+ public void rotate (int [] nums , int k ) {
63
+ k = k % nums .length ;
64
+ rotate (nums , 0 , nums .length - k - 1 );
65
+ rotate (nums , nums .length - k , nums .length - 1 );
66
+ rotate (nums , 0 , nums .length - 1 );
67
+ }
68
+
69
+ private void rotate (int [] nums , int start , int end ) {
70
+ while (start < end ) {
71
+ nums [start ] ^= nums [end ];
72
+ nums [end ] ^= nums [start ];
73
+ nums [start ] ^= nums [end ];
74
+ start ++;
75
+ end --;
76
+ }
77
+ }
78
+
79
+ }
84
80
}
85
81
86
82
0 commit comments