File tree 1 file changed +29
-8
lines changed
1 file changed +29
-8
lines changed Original file line number Diff line number Diff line change 3
3
void rotate (int nums [], int n , int k ) {
4
4
int i , j , c ;
5
5
i = j = 0 ;
6
- int x = nums [i ];
6
+ int pre = nums [i ];
7
7
int t ;
8
- for (c = 0 ; c < n ; c ++ )
9
- {
8
+ for (c = 0 ; c < n ; c ++ ) {
10
9
i = (i + k ) % n ;
11
10
t = nums [i ];
12
- nums [i ] = x ;
13
- x = t ;
14
- if (i == j )
15
- {
11
+ nums [i ] = pre ;
12
+ pre = t ;
13
+ if (i == j ) {
16
14
i = ++ j ;
17
- x = nums [i ];
15
+ pre = nums [i ];
16
+ }
17
+ }
18
+ }
19
+
20
+ /*
21
+ Cyclic group generator to do matrix transpose.
22
+ use a 1-dimension array to reprents m*n matrix.
23
+ the result is an n*m matrix.
24
+ */
25
+
26
+ void transpose (int * matrix , int m , int n ) {
27
+ int i , j , c ;
28
+ i = j = 0 ;
29
+ int pre = matrix [0 ];
30
+ int t ;
31
+ for (c = 0 ; c < m * n - 1 ; c ++ ) { /* pay attention it's m*n-1 */
32
+ i = (i % n ) * m + (i / n );
33
+ t = matrix [i ];
34
+ matrix [i ] = pre ;
35
+ pre = t ;
36
+ if (i == j ) {
37
+ i = ++ j ;
38
+ pre = matrix [i ];
18
39
}
19
40
}
20
41
}
You can’t perform that action at this time.
0 commit comments