leetcode:Spiral Matrix II
来源:互联网 发布:php utf8转换为gbk 编辑:程序博客网 时间:2024/06/05 03:44
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Have you met this question in a real interview?
Yes
No
Discuss
class Solution {public:vector<vector<int> > generateMatrix(int n) { vector<vector<int> > retVtr(n, vector<int>(n)); if (n == 0) { return retVtr; } int offset = 0; int N = n*n; int curVal = 1; while (curVal <= N) { if (curVal == N) { retVtr[offset][offset] = curVal; break; } for (int idx=offset; idx<n-1-offset && curVal<=N; idx++) retVtr[offset][idx] = curVal++; for (int idx=offset; idx<n-1-offset && curVal<=N; idx++) retVtr[idx][n-1-offset] = curVal++; for (int idx=n-1-offset; idx>offset && curVal<=N; idx--) retVtr[n-1-offset][idx] = curVal++; for (int idx=n-1-offset; idx>offset && curVal<=N; idx--) retVtr[idx][offset] = curVal++; offset++; } return retVtr;}};
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
- protoc 文件android报错的原因
- 揭秘jbpm流程引擎内核设计思想及构架
- 利用Solr搭建企业搜索平台之一-Solr运行
- Matlab各种排序函数
- static详解
- leetcode:Spiral Matrix II
- 11月份学习总结
- 安卓下的消息机制
- 利用SOLR搭建企业搜索平台 之二(中文分词)
- VC6.0MFC动态加载位图中出现的问题
- DNS递归和迭代解析
- 网络工具wget被发现存在安全漏洞(CVE-2014-4877)
- Android学习入门例子:打造Android版手机视频监控系统
- Matlab各种最值问题