NC210. 螺旋矩阵(二)
描述
示例1
输入:
3
输出:
[[1,2,3],[8,9,4],[7,6,5]]
示例2
输入:
1
输出:
[[1]]
C++ 解法, 执行用时: 2ms, 内存消耗: 424KB, 提交时间: 2021-11-25
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型vector<vector<>> */ vector<vector<int> > Matrix(int n) { // write code here vector<vector<int> > res(n,vector<int>(n)); int k=1; int left=0,right=n-1; int up=0,down=n-1; while(true) { for(int i=left;i<=right;++i) res[up][i]=k++; if(++up>down) break; for(int i=up;i<=down;++i) res[i][right]=k++; if(--right<left) break; for(int i=right;i>=left;--i) res[down][i]=k++; if(--down<up) break; for(int i=down;i>=up;--i) res[i][left]=k++; if(++left>right) break; } return res; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 308KB, 提交时间: 2022-05-26
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型vector<vector<>> */ vector<vector<int> > Matrix(int n) { // write code here vector<vector<int>> ans(n, vector<int>(n, 0)); int left = 0, right = n - 1, up = 0, down = n - 1; int begin = 1, end = n * n; while (begin <= end) { for (int i = left; i <= right; ++i) { ans[up][i] = begin++; } up++; for (int i = up; i <= down; ++i) { ans[i][right] = begin++; } right--; for (int i = right; i >= left; --i) { ans[down][i] = begin++; } down--; for (int i = down; i >= up; --i) { ans[i][left] = begin++; } left++; } return ans; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 384KB, 提交时间: 2022-07-12
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型vector<vector<>> */ vector<vector<int> > Matrix(int n) { // write code here int num=1; int l1=0,l2=n-1,r1=0,r2=n-1; vector<vector<int>>v(n,vector<int>(n)); while(num<=n*n) { for(int i=r1;i<=r2;++i) { v[l1][i]=num; ++num; } if(num>n*n)break; ++l1; for(int i=l1;i<=l2;++i) { v[i][r2]=num; ++num; } if(num>n*n)break; --r2; for(int i=r2;i>=r1;--i) { v[l2][i]=num; ++num; } if(num>n*n)break; --l2; for(int i=l2;i>=l1;--i) { v[i][r1]=num; ++num; } if(num>n*n)break; ++r1; } return v; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 384KB, 提交时间: 2022-03-14
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型vector<vector<>> */ vector<vector<int>> Matrix(int n) { vector<vector<int>> ans(n,vector<int>(n,0)); int top=0,bottom=n-1,left=0,right=n-1,count=1; while(top<=bottom&&left<=right) { for(int j=left;j<=right;j++) { ans[top][j]=count; count++; } for(int i=top+1;i<=bottom;i++) { ans[i][right]=count; count++; } if(left<right&&top<bottom) { for(int j=right-1;j>=left;j--) { ans[bottom][j]=count; count++; } for(int i=bottom-1;i>top;i--) { ans[i][left]=count; count++; } } left++;right--;top++;bottom--; } return ans; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 384KB, 提交时间: 2021-11-26
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型vector<vector<>> */ int M[22][22]; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; vector<vector<int> > Matrix(int n) { // write code here vector<vector<int> > matrix; memset(M,-1,sizeof M); f(0,0,1,n,1); for(int i = 0 ; i < n ; i ++) { vector<int> temp; for(int j = 0 ; j < n ; j ++) { temp.push_back(M[i][j]); } matrix.push_back(temp); } return matrix; } void f(int x,int y,int num,int n,int d){ for (int k = 0; k < n * n; k ++ ) { M[x][y] = num++; int a = x + dx[d], b = y + dy[d]; if (a < 0 || a >= n || b < 0 || b >= n || M[a][b] != -1) { d = (d + 1) % 4; a = x + dx[d], b = y + dy[d]; } x = a, y = b; } } };