Movatterモバイル変換


[0]ホーム

URL:


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

Commita1c8649

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent60b8388 commita1c8649

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

‎0054_spiral_matrix/spiral_matrix.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include<stdio.h>
22
#include<stdlib.h>
33

4+
45
/**
56
** Note: The returned array must be malloced, assume caller calls free().
67
**/
7-
staticint*spiralOrder(int**matrix,intmatrixSize,int*matrixColSize,int*returnSize)
8+
int*spiralOrder(int**matrix,intmatrixSize,int*matrixColSize,int*returnSize)
89
{
910
if (matrixSize==0) {
1011
*returnSize=0;

‎0054_spiral_matrix/spiral_matrix.cc‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ using namespace std;
55
classSolution {
66
public:
77
vector<int>spiralOrder(vector<vector<int>>& matrix) {
8-
if (matrix.empty()) {
9-
return vector<int>();
10-
}
11-
8+
vector<int> res;
129
int hor_top =0;
13-
int hor_bottom = matrix.size();
10+
int hor_bottom = matrix.size() -1;
1411
int ver_left =0;
15-
int ver_right = matrix[0].size();
12+
int ver_right = matrix[0].size() -1;
1613
int direction =0;
17-
vector<int> res;
18-
1914
while (hor_top <= hor_bottom && ver_left <= ver_right) {
2015
switch (direction) {
2116
case0:

‎0059_spiral_matrix_ii/spiral_matrix.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* The sizes of the arrays are returned as *returnColumnSizes array.
88
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
99
*/
10-
staticint**generateMatrix(intn,int*returnSize,int**returnColumnSizes)
10+
int**generateMatrix(intn,int*returnSize,int**returnColumnSizes)
1111
{
1212
inti;
1313
int**matrix=malloc(n*sizeof(int*));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<bits/stdc++.h>
2+
3+
usingnamespacestd;
4+
5+
classSolution {
6+
public:
7+
vector<vector<int>>generateMatrix(int n) {
8+
vector<vector<int>>matrix(n, vector<int>(n));
9+
int direction =0;
10+
int hor_top =0;
11+
int hor_bottom = n -1;
12+
int ver_left =0;
13+
int ver_right = n -1;
14+
int num =0;
15+
while (num < n * n) {
16+
switch (direction) {
17+
case0:
18+
for (int i = ver_left; i <= ver_right; i++) {
19+
matrix[hor_top][i] = ++num;
20+
}
21+
hor_top++;
22+
break;
23+
case1:
24+
for (int i = hor_top; i <= hor_bottom; i++) {
25+
matrix[i][ver_right] = ++num;
26+
}
27+
ver_right--;
28+
break;
29+
case2:
30+
for (int i = ver_right; i >= ver_left; i--) {
31+
matrix[hor_bottom][i] = ++num;
32+
}
33+
hor_bottom--;
34+
break;
35+
case3:
36+
for (int i = hor_bottom; i >= hor_top; i--) {
37+
matrix[i][ver_left] = ++num;
38+
}
39+
ver_left++;
40+
break;
41+
}
42+
direction++;
43+
direction %=4;
44+
}
45+
return matrix;
46+
}
47+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp