【leetcode】Spiral Matrix II
来源:互联网 发布:网络加速器翻墙 编辑:程序博客网 时间:2024/05/16 01:53
Spiral Matrix II
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 ]]
观察上述的矩阵,基本就是按顺序输出就可以了,注意二维vector的建立~
class Solution {public: vector<vector<int> > generateMatrix(int n) { vector<vector<int> > result(n, vector<int>(n)); int first=0,last=n-1; int num=1; while(first<last) { for(int i=first;i<last;i++) {result[first][i]=num;num++;} for(int j=first;j<last;j++) {result[j][last]=num;num++;} for(int i=last;i>first;i--) {result[last][i]=num;num++;} for(int j=last;j>first;j--) {result[j][first]=num;num++;} first++; last--; } if(first==last)result[first][first]=num; return result; }};
0 0
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode]Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Leetcode: Spiral Matrix II
- 【LeetCode】Spiral Matrix II
- Leetcode Spiral Matrix II
- [LeetCode] Spiral Matrix II
- LeetCode | Spiral Matrix II
- LeetCode Spiral Matrix II
- Leetcode: Spiral Matrix II
- Spiral Matrix II -- LeetCode
- 多版本处理
- hdu2825 AC自动机+状态压缩DP
- SequenceList(顺序表)
- Flume 1.5.2 User Guide
- 河南省ACM程序设计 物资调度
- 【leetcode】Spiral Matrix II
- swing awt可视化操作插件WindowBuilder的安装
- 机房重构之组合查询
- 7
- bzoj 4034: [HAOI2015]T2
- 搭配nodejs mongodb开发环境
- 一个html5页面的思考
- Git操作-标签
- 和为S的连续正数序列