[LeetCode] Spiral Matrix II 螺旋矩阵之二
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
类似题目:
参考资料:
https://leetcode.com/problems/spiral-matrix-ii/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Spiral Matrix II 螺旋矩阵之二的更多相关文章
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [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 ...
- Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [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 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 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 ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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 ...
随机推荐
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...
- 数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇
<数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇>一文让读者了解了 HT的 2D 拓扑图组件使用,本文将对 HT 的 3D 功能做个综合性的介绍,以便初学者可快速上手使用 HT ...
- 【那些年关于java多态应用】
1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal { public abstract void Say();} 子类: public class ...
- GridView中显示时间日期格式问题
以下都是GridView基本常用的日期,时间格式 形式 语法 结果 注释 数字 {0:N2} 12.36 数字 {0:N0} 13 货币 {0:c2} $12.36 货币 {0:c4} $ ...
- JavaWeb_day08_EL JSTL
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day08 EL JSTL EL表达式 语法:${} ...
- 解决 Tomcat Server in Eclipse unable to start within 45 seconds 不能启动的问题
1.在 Eclipse 下方 Servers TAB页,双击 "Tomcat 7.0 at localhost": 2.在右上角处点开 Timeouts 的设定,修改Start( ...
- JAVA基础培训(isoft)
我们
- ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...
- Scala 包
包的绝对地址_root_.开始 如_root_.scala.collection.mutable.ArrayBuffer
- iOS多线程之7.NSOperation的初识
NSOperation和GCD一样,不用我们管理线程的生命周期,加锁等问题,只要把操作封装进NSOperation中,系统会自动帮我们创建线程,执行操作.而且他是面向对象的,我们看起来更容易理解,使用 ...