problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::spiral_matrix_ii::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< vector< int > > generateMatrix (int n)
 

详细描述

在文件 leetcode.h2646 行定义.

成员函数说明

◆ generateMatrix()

vector< vector< int > > leetcode::spiral_matrix_ii::Solution::generateMatrix ( int  n)
static

在文件 leetcode.cpp7156 行定义.

7156 {
7157 vector ans(n, vector(n, 0));
7158 int dir = 0;// 0-right 1-down 2-left 3->up
7159 int c = 1;
7160 int x = 0;
7161 int y = 0;
7162 for(int i = 0; i < n * n; i++) {
7163 ans[x][y] = c++;
7164 bool flag = true;
7165 SLCT:
7166 int nx = x;
7167 int ny = y;
7168 switch(dir) {
7169 case 0:
7170 ny++;
7171 break;
7172 case 1:
7173 nx++;
7174 break;
7175 case 2:
7176 ny--;
7177 break;
7178 case 3:
7179 nx--;
7180 break;
7181 }
7182 if(flag && (nx < 0 || nx >= n || ny < 0 || ny >= n || ans[nx][ny] != 0)) {
7183 dir++;
7184 dir %= 4;
7185 flag = false;
7186 goto SLCT;
7187 }
7188 x = nx;
7189 y = ny;
7190 }
7191 return ans;
7192 }

被这些函数引用 leetcode::spiral_matrix_ii::TEST().


该类的文档由以下文件生成: