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 ]
]
class Solution {
public:
vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n,vector<int>(n,));
if (n == ) return res;
int i = ;
int rowS = ,rowE = n - ,colS = ,colE = n -;
while(i <= n * n)
{
for (int j = colS;j <= colE;++j)
{
res[rowS][j] = i++;
}
rowS++; for (int j = rowS;j <= rowE;++j)
{
res[j][colE] = i++;
}
colE--; for (int j = colE;j >= colS;--j)
{
res[rowE][j] = i++;
}
rowE--; for (int j = rowE;j >= rowS;--j)
{
res[j][colS] = i++;
}
colS++;
}
return res;
}
};

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

Example 1:

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

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = m ? matrix[].size() : , u = , d = m - , l = , r = n - , p = ;
vector<int> order(m * n);
while (u <= d && l <= r) {
for (int col = l; col <= r; col++) {
order[p++] = matrix[u][col];
}
if (++u > d) {
break;
}
for (int row = u; row <= d; row++) {
order[p++] = matrix[row][r];
}
if (--r < l) {
break;
}
for (int col = r; col >= l; col--) {
order[p++] = matrix[d][col];
}
if (--d < u) {
break;
}
for (int row = d; row >= u; row--) {
order[p++] = matrix[row][l];
}
if (l++ > r) {
break;
}
}
return order;
}
};

54. Spiral Matrix && 59. Spiral Matrix II的更多相关文章

  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@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  4. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. [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 ...

  6. 54. 59. Spiral Matrix

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

  7. LeetCode第[54]题(Java):Spiral Matrix

    题目:螺旋矩阵 难度:Medium 题目内容: Given a matrix of m x n elements (m rows, n columns), return all elements of ...

  8. LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵

    题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...

  9. 矩阵螺旋遍历Spiral Matrix,Spiral Matrix2

    SpiralMatrix: Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

随机推荐

  1. linux-touch 、mkdir、rm、pwd、which、locate、whatis

    1.touch: 创建空文件,修改文件时间戳信息 atime(access time):最近访问文件内容时间 mtime(modify time):最近修改文件内容时间 ctime(change ti ...

  2. 【系列专题】ECMAScript 重温系列(10篇全)

    ES6 系列ECMAScript 2015 [ES]150-重温基础:ES6系列(一) [ES]151-重温基础:ES6系列(二) [ES]152-重温基础:ES6系列(三) [ES]153-重温基础 ...

  3. 【玩转SpringBoot】用好条件相关注解,开启自动配置之门

    自动配置隐含两层含义,要搞清楚 上帝让程序员的发量减少,是为了让他变得更聪明,如果有一天聪明到了极点,那就是绝顶聪明. 据说在大脑高速运转下,这样更有利于散热,不至于核心温度过高而产生告警. 聪明的大 ...

  4. org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'taglib' not found

    tomcat7,部署tomcat6下的项目统,报tomcat 7: IllegalArgumentException: taglib definitionnotconsistentwithspecif ...

  5. 小公举-linux的计算器

    1.一个方便的linux计算器,精巧而强大bc 2..进行简单的四则运算 3.连续的四则运算 4.大数运算 5.求次幂和余数 6.如果要执行小数计算呢,需要设置scale=number ,number ...

  6. 分布式系统的发展演变以及RPC简介

    场景 什么是分布式系统 分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统. 分布式系统是建立在网络之上的软件系统. 注: 博客: https://blog.csdn.net/b ...

  7. VMware kali虚拟机环境配置

    编译内核 (1)执行命令uname -r以查看内核版本. (2)执行命令apt-cache search linux-headers查看是否安装内核头文件. (3)  如果uname -r出现的内容在 ...

  8. 1w+的心路历程

    鬼知道我是如何坚持下来的,如果非要找个理由,那或许是所谓的热爱. 公众号转眼间写了三年.写的内容围绕着安卓技术,源码剖析,生活感悟,职场人生. 很庆幸的是,得到大家的支持,每一条留言都会是一次交流,看 ...

  9. Android 开机充电图标和充电动画

    首先驱动需要先获取到2个power supply kernel\msm-3.18\drivers\usb\phy\phy-msm-usb.c motg->usb_psy.name = " ...

  10. 离线安装Mariadb

    CentOS7.4开发站系统和红旗Asianux-7.3离线安装Mariadb 安装 需要Root权限 # 解压离线rpm包 tar -xvf Mariadb5.5.56.tar cd Mariadb ...