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. Python学习笔记—自动化部署【新手必学】

      前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨   目录 pexpect fabric pexpect P ...

  2. ARTS-S gitlab与jenkins实现持续集成

    jenkins配制 系统管理->管理插件->可选插件->选择安装 Gitlab Hook Plugin和Build Authorization Token Root Plugin插件 ...

  3. 使用iCamera 测试MT9M001 130w高分辨率摄像头说明

    该摄像头默认分辨率为1280*1024,即不设置任何寄存器参数,只要给该模块提供时钟,就可以输出. 在这里 我们可以通过右侧寄存器栏动态调整各寄存器 观察效果. 0x09寄存器可以调整曝光值,可以根据 ...

  4. nmap扫描进阶、msfconsole攻击入门(网安全实训第二天)

    本期内容:nmap扫描.msfconsole攻击入门 1. nmap扫描进阶 2.msfconsole攻击入门 1.nmap扫描进阶 (1)nmap命令 nmap --sP -iL abin.txt ...

  5. 10分钟理解BFC原理

    10 分钟理解 BFC 原理 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: 普通流 (normal flow) 在普通流中,元素按照 ...

  6. Linux内核构建过程

    构建内核 # shell 执行如下指令make zImage 全局变量 srctree    := $(if $(KBUILD_SRC),$(KBUILD_SRC),$(CURDIR))objtree ...

  7. 深度理解return具体用法

    ''' 下面我们来更加深度的理解return具体用法!!! return 默认返回None return 可以返回任意数据类型的数据 return 返回多个值的时候,会以元祖的形式把多个值包在一起 ' ...

  8. Http协议 & Servlet

    Http协议&Servlet Http协议 什么是协议 双方在交互.通讯的时候遵守的一种规范.规则. http协议 针对网络上的客户端与服务器端在执行http请求的时候,遵守的一种规范.其实就 ...

  9. java之构造器

    1.构造方法的作用:在new创建对象时为其赋值. 2.构造方法的分类: ①无参构造public 同类名(){},有参构造public 同类名(参数列表){语句}. ②构造方法没有方法名,没有返回值类型 ...

  10. 《Java基础知识》序列化与反序列化详解

    序列化的作用:为了不同jvm之间共享实例对象的一种解决方案.由java提供此机制. 序列化应用场景: 1. 分布式传递对象. 2. 网络传递对象. 3. tomcat关闭以后会把session对象序列 ...