加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Leetcode 59 Spiral Matrix II

发布时间:2020-12-13 21:17:29 所属栏目:PHP教程 来源:网络整理
导读:Given an integer n ,generate a square matrix filled with elements from 1 to n 2 in spiral order. For example, Given n = 3 , You should return the following matrix: [ [ 1,2,3 ],[ 8,9,4 ],[ 7,6,5 ]] 方阵蛇形填数 和上1道蛇形取数差不多。 http:

Given an integer n,generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[ [ 1,2,3 ],[ 8,9,4 ],[ 7,6,5 ] ]
方阵蛇形填数

和上1道蛇形取数差不多。

http://blog.csdn.net/accepthjp/article/details/52577112

class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<int> row(n,0); vector<vector<int>> result(n,row); int rows=n,cols=n,cnt=0; for(int x=0,y=0;x<rows && y<cols;x++,y++) { for(int i=y;i<cols;i++) result[x][i]=++cnt; for(int i=x+1;i<rows;i++) result[i][cols⑴]=++cnt; for(int i=cols⑵;i>=y;i--) result[rows⑴][i]=++cnt; for(int i=rows⑵;i>x;i--) result[i][y]=++cnt; rows--; cols--; } return result; } };


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读