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

For example,

Given n = 3,

You should return the following matrix:

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

思路完全同 54. Spiral Matrix(中等).

自个代码:

vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> A(n, vector<int>(n)); //n行n列,动态的 // Normal Case
int rowStart = 0;
int rowEnd = n - 1;
int colStart = 0;
int colEnd = n - 1;
int num = 1; //change while (rowStart <= rowEnd && colStart <= colEnd) {
for (int i = colStart; i <= colEnd; i++) {
A[rowStart][i] = num++; //change
}
rowStart++; for (int i = rowStart; i <= rowEnd; i++) {
A[i][colEnd] = num++; //change
}
colEnd--; for (int i = colEnd; i >= colStart; i--) {
if (rowStart <= rowEnd)
A[rowEnd][i] = num++; //change
}
rowEnd--; for (int i = rowEnd; i >= rowStart; i--) {
if (colStart <= colEnd)
A[i][colStart] = num++; //change
}
colStart++;
}
return A;
}

59. Spiral Matrix II(中等,同54题)的更多相关文章

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

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

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

  3. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  4. 【leetcode】59.Spiral Matrix II

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

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

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

  6. Leetcode#59 Spiral Matrix II

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

  7. 59. Spiral Matrix II

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

  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 螺旋矩阵 II

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

随机推荐

  1. Java设计模式(八)Proxy代理模式

    一.场景描述 代理在生活中并不少见,租房子需要找中介,打官司需要找律师,很多事情我们需要找专业人士代理我们做,另一方面,中介和律师也代理了房东.法律程序与我们打交道. 当然,设计模式中的代理与广义的代 ...

  2. poj 2945 Find the Clones

    https://vjudge.net/problem/POJ-2945 题意: 给出n个长度相同的DNA序列,如果一个DNA序列出现过两次,那么就有说明它被复制了一次.问被复制0次,1次,2次--n- ...

  3. window7下配置python2.7+tornado3.3开发环境

    发现之前写太繁琐..这里分享下同学的方法 1,安装 Python 2.7.x 版本地址:https://www.python.org/downloads/release/python-278/2,安装 ...

  4. SQLContext、HiveContext自定义函数注册

    本文简单介绍两种往SQLContext.HiveContext中注册自定义函数方法. 下边以sqlContext为例,在spark-shell下操作示例: scala> sc res5: org ...

  5. 初识 SpringMVC

    1.Spring MVC 的工作流程 1.web请求被 前端控制器(DispatcherServlet)拦截 2.DispatcherServlet调用 映射处理器(HandelerMapping)查 ...

  6. Entry的验证

    Entry组件是支持验证输入的合法性的, 比如要求输入数字,你输入了字母就是非法. 实现该功能,需要通过设置validate,validatecommand,invalidcommand选项. 1.首 ...

  7. Text-查找文本

    from tkinter import * master=Tk() text=Text(master,width=30,height=5) text.pack() text.insert(INSERT ...

  8. *Boosting*笔记

    集成算法之boosting 集成方法  1. Parallel methods:   1. bagging   2. Random Forest  2. Sequence methods:   1. ...

  9. MySQL之SQL语句的优化

    仅供自己学习 结论写在前面: 1.尽量避免进行全表扫描,可以给where和order by涉及的列上建立索引 2.尽量在where子句中使用 !=或<>操作符,因为这样会导致引擎放弃索引而 ...

  10. Java程序优化之替换swtich

    关键字switch语句用于多条件判断,功能类似于if-else语句,两者性能也差不多,不能说switch会降低系统性能.在绝大部门情况下,switch语句还是有性能提升空间的. 但是在项目代码中,如果 ...