1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
59. 螺旋矩阵 II.cpp 2.62 KB
一键复制 编辑 原始数据 按行查看 历史
TieNan2019 提交于 2021-02-24 11:09 . Create 59. 螺旋矩阵 II.cpp
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int val = 1;
vector<vector<int>> matrix(n, vector<int>(n));
int rUpper = 0, rDowner = n - 1;
int cLeft = 0, cRight = n - 1;
int r = 0, c = 0;
enum {
RIGHT,
DOWN,
LEFT,
UP
} direct;
direct = RIGHT;
while (val <= n * n) {
matrix[r][c] = val;
switch(direct) {
case RIGHT:
if (c == cRight) {
r++;
direct = DOWN;
rUpper++;
}
else
c++;
break;
case DOWN:
if (r == rDowner) {
c--;
direct = LEFT;
cRight--;
}
else
r++;
break;
case LEFT:
if (c == cLeft) {
r--;
direct = UP;
rDowner--;
}
else
c--;
break;
case UP:
if (r == rUpper) {
c++;
direct = RIGHT;
cLeft++;
}
else
r--;
break;
default:
break;
}
val++;
}
return matrix;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助