Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cf5dbd3

Browse files
committed
"Rotate Array": cyclic group generator
1 parent e3d3b9e commit cf5dbd3

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

‎src/189.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,39 @@
33
void rotate(int nums[], int n, int k) {
44
int i, j, c;
55
i = j = 0;
6-
int x = nums[i];
6+
int pre = nums[i];
77
int t;
8-
for(c = 0; c < n; c++)
9-
{
8+
for(c = 0; c < n; c++) {
109
i = (i + k) % n;
1110
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) {
1614
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];
1839
}
1940
}
2041
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /