不知道一年后会成长成什么样,只感觉好难好难。有好多东西要学,源码也看不懂,项目也不会做。
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> vec(n, vector<int>(n, 0));
int num = 1;
for(int i = 0; i < n; ++i){
if(vec[i][i] == 0) insert(i, num, n, vec);
}
return vec;
}
void insert(int curPosition, int& num, int n, vector<vector<int>>& vec){
int i = curPosition, j = curPosition;// i是行,j是列
for(;j < n; ++j){//向右
if(vec[i][j] == 0){
vec[i][j] = num++;
}else{
break;
}
}
++i;
--j;
for(;i < n; ++i){//向下
if(vec[i][j] == 0){
vec[i][j] = num++;
}else{
break;
}
}
--j;
--i;
for(;j >= 0; --j){//向左
if(vec[i][j] == 0){
vec[i][j] = num++;
}else{
break;
}
}
--i;
++j;
for(;i >= 0; --i){//向上
if(vec[i][j] == 0){
vec[i][j] = num++;
}else{
break;
}
}
}
};
标签:59,++,矩阵,else,II,int,num,vec,-- From: https://www.cnblogs.com/llllmz/p/18416706