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

Example:

Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
 

此题跟之前那道 Spiral Matrix 本质上没什么区别,就相当于个类似逆运算的过程,这道题是要按螺旋的顺序来填数,由于给定矩形是个正方形,我们计算环数时用 n / 2 来计算,若n为奇数时,此时最中间的那个点没有被算在环数里,所以最后需要单独赋值,还是下标转换问题是难点,参考之前 Spiral Matrix 的讲解来转换下标吧,参见代码如下:

解法一:

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n));
int val = , p = n;
for (int i = ; i < n / ; ++i, p -= ) {
for (int col = i; col < i + p; ++col)
res[i][col] = val++;
for (int row = i + ; row < i + p; ++row)
res[row][i + p - ] = val++;
for (int col = i + p - ; col >= i; --col)
res[i + p - ][col] = val++;
for (int row = i + p - ; row > i; --row)
res[row][i] = val++;
}
if (n % != ) res[n / ][n / ] = val;
return res;
}
};

当然我们也可以使用下面这种简化了坐标转换的方法,博主个人还是比较推崇下面这种解法,不容易出错,而且好理解,参见代码如下:

解法二:

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n));
int up = , down = n - , left = , right = n - , val = ;
while (true) {
for (int j = left; j <= right; ++j) res[up][j] = val++;
if (++up > down) break;
for (int i = up; i <= down; ++i) res[i][right] = val++;
if (--right < left) break;
for (int j = right; j >= left; --j) res[down][j] = val++;
if (--down < up) break;
for (int i = down; i >= up; --i) res[i][left] = val++;
if (++left > right) break;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/59

类似题目:

Spiral Matrix

参考资料:

https://leetcode.com/problems/spiral-matrix-ii/

https://leetcode.com/problems/spiral-matrix-ii/discuss/22392/C%2B%2B-template-for-Spiral-Matrix-and-Spiral-Matrix-II

https://leetcode.com/problems/spiral-matrix-ii/discuss/22289/My-Super-Simple-Solution.-Can-be-used-for-both-Spiral-Matrix-I-and-II

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Spiral Matrix II 螺旋矩阵之二的更多相关文章

  1. [Leetcode] spiral matrix ii 螺旋矩阵

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

  2. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  3. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  4. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  5. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  6. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  7. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  9. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

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

随机推荐

  1. 基于 HTML5 的 Web SCADA 报表

    背景 最近在一个 SCADA 项目中遇到了在 Web 页面中展示设备报表的需求.一个完整的报表,一般包含了筛选操作区.表格.Chart.展板等多种元素,而其中的数据表格是最常用的控件.在以往的工业项目 ...

  2. React-Native学习系列(一)

    近段时间一直在忙,所以博客也没有更新,这两天我翻了一下写的这几篇博客,感觉写的都很片面,所以,我想重新写一个系列教程,从最基础的开始,来让大家更容易学会React-Native. 这个系列大部分只介绍 ...

  3. WinForm拖动没有标题栏窗体的方法

    建立窗体的名称修改为:Form_HoverTree 文后附有源码下载. 主要代码: Point _HoverTreePosition; public Form_HoverTree() { Initia ...

  4. 【无私分享:ASP.NET CORE 项目实战(第四章)】Code First 创建数据库和数据表

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 本章我们来介绍下Asp.net Core 使用 CodeFirst 创建数据库和表,通过 控制台 和 dotnet ef 两种 ...

  5. 基于redis的处理session的方法

    一个基于redis的处理session的方法,如下. <?php class Session_custom { private $redis; // redis实例 private $prefi ...

  6. Servlet3.0的异步

    servlet之前的操作同时同步的,就是按照这样的一个流程来走的: 1.请求根据一个路径路由到一个servlet中, 2.servlet获取一系列的参数 3.执行一系列的逻辑(花费时间所占的比重也更大 ...

  7. js条件判断时隐式类型转换

    Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...

  8. 深入理解javascript函数参数与闭包(一)

    在看此文章,希望先阅读关于函数基础内容 函数定义与函数作用域 的章节,因为这篇文章或多或少会涉及函数基础的内容,而基础内容,我放在函数定义函数作用域 章节. 本文直接赘述函数参数与闭包,若涉及相关知识 ...

  9. SharePoint文档库文件夹特殊字符转义

    当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个), 或者以.开头或者结束,或者包含 ...

  10. 安装pip

    1. 安装pip. 我们同样需要在Python的官网上去下载,下载地址是: https://pypi.python.org/pypi/pip#downloads 2. 解压. 解压pip-9.0.1. ...