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 468a05f

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 8354b9a commit 468a05f

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

‎054_spiral_matrix/spiral_matrix.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
/**
55
** Note: The returned array must be malloced, assume caller calls free().
66
**/
7-
static int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize)
7+
static int* spiralOrder(int** matrix, int matrixSize, int *matrixColSize, int*returnSize)
88
{
9+
if (matrixSize == 0) {
10+
*returnSize = 0;
11+
return NULL;
12+
}
13+
914
int hor_top = 0;
10-
int hor_bottom = matrixRowSize - 1;
15+
int hor_bottom = matrixSize - 1;
1116
int ver_left = 0;
12-
int ver_right = matrixColSize - 1;
13-
int *nums = malloc(matrixRowSize * matrixColSize * sizeof(int));
17+
int ver_right = matrixColSize[0] - 1;
18+
int *nums = malloc(matrixSize * matrixColSize[0] * sizeof(int));
1419
int count = 0;
1520
int i, direction = 0;
1621

@@ -47,27 +52,31 @@ static int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize)
4752
direction %= 4;
4853
}
4954

55+
*returnSize = count;
5056
return nums;
5157
}
5258

5359
int main(int argc, char **argv)
5460
{
5561
int i, j, count = 0;
5662
int row = 3;
57-
int col = 3;
63+
int *cols = malloc(row*sizeof(int));
5864
int **mat = malloc(row * sizeof(int *));
5965
for (i = 0; i < row; i++) {
60-
mat[i] = malloc(col * sizeof(int));
61-
for (j = 0; j < col; j++) {
66+
cols[i] = row;
67+
mat[i] = malloc(cols[i] * sizeof(int));
68+
for (j = 0; j < cols[i]; j++) {
6269
mat[i][j] = ++count;
6370
printf("%d ", mat[i][j]);
6471
}
6572
printf("\n");
6673
}
67-
int *nums = spiralOrder(mat, row, col);
68-
for (i = 0; i < row * col; i++) {
74+
75+
int *nums = spiralOrder(mat, row, cols, &count);
76+
for (i = 0; i < count; i++) {
6977
printf("%d ", nums[i]);
7078
}
7179
printf("\n");
80+
7281
return 0;
7382
}

‎054_spiral_matrix/spiral_matrix.cc‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
8+
if (matrix.empty()) {
9+
return vector<int>();
10+
}
11+
12+
int hor_top = 0;
13+
int hor_bottom = matrix.size();
14+
int ver_left = 0;
15+
int ver_right = matrix[0].size();
16+
int direction = 0;
17+
vector<int> res;
18+
19+
while (hor_top <= hor_bottom && ver_left <= ver_right) {
20+
switch (direction) {
21+
case 0:
22+
for (int i = ver_left; i <= ver_right; i++) {
23+
res.push_back(matrix[hor_top][i]);
24+
}
25+
hor_top++;
26+
break;
27+
case 1:
28+
for (int i = hor_top; i <= hor_bottom; i++) {
29+
res.push_back(matrix[i][ver_right]);
30+
}
31+
ver_right--;
32+
break;
33+
case 2:
34+
for (int i = ver_right; i >= ver_left; i--) {
35+
res.push_back(matrix[hor_bottom][i]);
36+
}
37+
hor_bottom--;
38+
break;
39+
case 3:
40+
for (int i = hor_bottom; i >= hor_top; i--) {
41+
res.push_back(matrix[i][ver_left]);
42+
}
43+
ver_left++;
44+
break;
45+
default:
46+
break;
47+
}
48+
direction++;
49+
direction %= 4;
50+
}
51+
52+
return res;
53+
}
54+
};

‎138_copy_list_with_random_pointer/copy_list.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ static struct Node *copyRandomList(struct Node *head)
2727
struct Node dummy;
2828
struct Node *prev = &dummy;
2929
prev->next = head;
30+
/* separate q list */
3031
for (p = head; p != NULL; p = q->next) {
3132
q = p->next;
32-
/* separate q list */
33+
/* restore p->next */
34+
p->next = q->next;
3335
prev->next = q;
3436
prev = q;
3537
}

0 commit comments

Comments
(0)

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