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)的更多相关文章

  1. 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 ...

  2. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  3. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  4. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  5. 59. Spiral Matrix II

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

  6. 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 ...

  7. 59. Spiral Matrix II(中等,同54题)

    Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...

  8. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  9. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

随机推荐

  1. Alpha阶段第1周/共2周 Scrum立会报告+燃尽图 03

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2248 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  2. 通过VNC连接远程服务器,然后登陆服务器上的虚拟机,出现键盘输入问题的解决方法

    前几天由于要在服务器上装一个虚拟机,然后就选择了vmware workstation,装好之后,进入虚拟机中的centOS系统,发现键盘上的Cpas Lock键不起作用,按下之后还是输入小写,而且按住 ...

  3. PostgreSQL逻辑复制槽

    Schema | Name | Result data type | Argument data types | Type ------------+------------------------- ...

  4. Linux:提示符PS1个性设置

    提示符PS1个性设置 1)默认PS1 echo $PS1 2)个性PS1 #去掉了默认显示的[]号#\e[1;34m\]\u:user名高亮显示并显示颜色#\e[5;33m\]\h:hostname主 ...

  5. 回测框架pybacktest简介(二)

    pybacktest 的疑点 第(一)节“教程”原文,是用 ipython notebook 写成,程序代码是一些片段组成. 为了阅读方便,合并在一起. 本文转载于:http://blog.csdn. ...

  6. Js 图片轮播渐隐效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. 文件和I/O流

    版权声明:本文为[博主](https://zhangkn.github.io)原创文章,未经博主同意不得转载.https://creativecommons.org/licenses/by-nc-sa ...

  8. leetcode:Insert Sort List

    问题描写叙述 对一个单链表进行插入排序,head指向第一个结点. 代码 /** * Definition for singly-linked list. * struct ListNode { * i ...

  9. BZOJ1252:序列终结者

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  10. (接上一条)解决ssh隧道断开自动重连的问题

    Sounds like you need autossh. This will monitor an ssh tunnel and restart it as needed. http://www.d ...