59. Spiral Matrix II (Array)
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 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > result(n, vector<int>(n,));
leftPos = ;
rightPos = n-;
topPos = ;
bottomPos = n-;
currentNum = ;
goWider(result,true);
return result;
}
void goWider(vector<vector<int> > &matrix, bool direct)
{
if(direct)
{
for(int i = leftPos; i<= rightPos; i++)
{
matrix[topPos][i] = currentNum++;
}
topPos++;
if(topPos > bottomPos) return;
goDeeper(matrix, true);
}
else
{
for(int i = rightPos; i>= leftPos; i--)
{
matrix[bottomPos][i] = currentNum++;
}
bottomPos--;
if(topPos > bottomPos) return;
goDeeper(matrix, false);
}
}
void goDeeper(vector<vector<int> > &matrix, bool direct)
{
if(direct)
{
for(int i = topPos; i<= bottomPos; i++)
{
matrix[i][rightPos]=currentNum++;
}
rightPos--;
if(leftPos > rightPos) return;
goWider(matrix, false);
}
else
{
for(int i = bottomPos; i>= topPos; i--)
{
matrix[i][leftPos] = currentNum++;
}
leftPos++;
if(leftPos > rightPos) return;
goWider(matrix, true);
}
}
private:
int currentNum;
int leftPos;
int rightPos;
int topPos;
int bottomPos;
};
思路II:向右向下向左向上,4个for循环。外套一个while,while的结束条件是4个for循环都不满足循环条件了。
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int len = n;
int num = ;
vector<vector<int>> ret(n, vector<int>(n,));
while(len>n-len){
for(int i = n-len; i < len; i++){
ret[n-len][i] = num++;
}
for(int j = n-len+; j < len; j++){
ret[j][len-] = num++;
}
for(int k = len-; k >= n-len; k--){
ret[len-][k] = num++;
}
for(int l = len-; l > n-len; l--){
ret[l][n-len] = num++;
}
len--;
}
return ret;
}
};
59. Spiral Matrix II (Array)的更多相关文章
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- LeetCode OJ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
随机推荐
- specialized English for automation-Lesson 2 Basic Circuits of Operational Amplifiers
排版有点乱.... ========================================================================= Operational Ampl ...
- timer Compliant Controller project (4)layout and gerber, paning
1 LAYOUT 2 Gerber 3 CAM350-Paining
- SQLSERVER 数据调度示例,调度数据到中间表或者历史表
USE [MeiDongPay_Test] GO /****** Object: StoredProcedure [dbo].[Job_BatchTransferOrderToMidst] Scrip ...
- windows下Java调用mysql的客户端备份和恢复
这种东西没啥好聊的,其实就是Java执行dos界面下的命令,不过有些要注意就是了,真实dos下面的命令和java调用的windows系统的接口其实还是有一点不同. /** * @param hostI ...
- cratedb joins 原理(官方文档)
JOINs are essential operations in relational databases. They create a link between rows based on c ...
- JSP动作指令
JSP动作指令 动作指令与编译指令不间,编译指令是通知 Servlet 引擎的处理消息,而动作指令只是运行时的脚本动作.编译指令在将JSP 编译成 Servlet 时起作用:处理指令通常可替换成 Ja ...
- RHEL6安装Oracle 11g R2
收藏PDF版质料请点这里:http://download.csdn.net/detail/jifeng3518/6464999 1.使用DVD做yum源1.1.新建dvd挂载目录[root@oracl ...
- bzo j4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据 结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的 ...
- java之RestTemplate的访问应用
一.REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.它是 ...
- varnish/squid/nginx cache 有什么不同?
SQUID 是功能最全面的,但是架构太老,性能不咋的Varnish 是内存缓存,速度一流,但是内存缓存也限制了其容量,缓存页面和图片一般是挺好的Nginx 本来是反向代理/web服务器,用了插件可以做 ...