Spiral Matrix

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

For example,
Given the following matrix:

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

You should return [1,2,3,6,9,8,7,4,5].

打印螺旋矩阵

逐个环的打印, 对于m *n的矩阵,环的个数是 (min(n,m)+1) / 2。对于每个环顺时针打印四条边。

注意的是:最后一个环可能只包含一行或者一列数据

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
int m = matrix.size(), n;
if(m != 0)n = matrix[0].size();
int cycle = m > n ? (n+1)/2 : (m+1)/2;//环的数目
vector<int>res; int a = n, b = m;//a,b分别为当前环的宽度、高度
for(int i = 0; i < cycle; i++, a -= 2, b -= 2)
{
//每个环的左上角起点是matrix[i][i],下面顺时针依次打印环的四条边
for(int column = i; column < i+a; column++)
res.push_back(matrix[i][column]);
for(int row = i+1; row < i+b; row++)
res.push_back(matrix[row][i+a-1]);
if(a == 1 || b == 1)break; //最后一个环只有一行或者一列
for(int column = i+a-2; column >= i; column--)
res.push_back(matrix[i+b-1][column]);
for(int row = i+b-2; row > i; row--)
res.push_back(matrix[row][i]);
}
return res;
}
};

Spiral Matrix II

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

For example,
Given n = 3,

You should return the following matrix:

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

本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵。同理,我们也是逐个环的填充,每个环顺时针逐条边填充                 本文地址

class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > matrix(n, vector<int>(n));
int a = n;//a为当前环的边长
int val = 1;
for(int i = 0; i < n/2; i++, a -= 2)
{
//每个环的左上角起点是matrix[i][i],下面顺时针依次填充环的四条边
for(int column = i; column < i+a; column++)
matrix[i][column] = val++;
for(int row = i+1; row < i+a; row++)
matrix[row][i+a-1] = val++;
for(int column = i+a-2; column >= i; column--)
matrix[i+a-1][column] = val++;
for(int row = i+a-2; row > i; row--)
matrix[row][i] = val++;
}
if(n % 2)matrix[n/2][n/2] = val;//n是奇数时,最后一个环只有一个数字
return matrix;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3774747.html

LeetCode:Spiral Matrix I II的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] Spiral Matrix 螺旋矩阵

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

  5. Spiral Matrix I & II

    Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...

  6. [LeetCode]Spiral Matrix 54

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

  7. LeetCode: Spiral Matrix 解题报告

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

  8. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  9. Leetcode Spiral Matrix II

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

随机推荐

  1. 关于MAC系统的DNSCrypt的设置教程

    DNSCrypt的设计完全是为了解决某些网站DNS系统被污染的问题. 如图是查看本地的DNS DNS系统是一个主要领域的安全保障,因为受损数据从一个DNS服务器会导致你的系统或者无法找到需要的服务器需 ...

  2. session的使用方法

    概念:session把客户资料存在服务器中,给浏览器一个加密凭证,每次登录生成的凭证都不相同,浏览器用cookie保存凭证.下次访问时服务器收到凭证后,打开文件读取session信息.session_ ...

  3. 校验两次密码一致的js代码

    function checkpasswd(){ var passwd = document.getElementByIdx_x_x_xx_x('passwd').value; var repasswd ...

  4. tar 压缩去除目录

    tar zcvf fd.tar.gz  *  --exclude=file1 --exclude=dir1 注意: 1.yes: --exclude=file1 no: --exclude file1 ...

  5. jsp_包含指令

    1.静态包含: <%@ include file="被包含的文件的路径"%> 2.动态包含: 不传递参数:<jsp:include page="{要包含 ...

  6. JavaScript 高性能笔记

    浏览器解析 JavaScript .CSS .DOM 时,一般都是单线程解析,所以,引用外部文件时的位置不同,UE体验也不同. 下面是 Yahoo 大牛 Nicholas C. Zakas 的 < ...

  7. Android性能优化方法(四)

    在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件.一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局.如果我们在 ...

  8. Robot Framework-工具简介及入门使用

    Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...

  9. struts框架学习过程中的问题

    1,错误: java.lang.NullPointerException: Module 'null' not found.错误原因,struts运行需要的.jar文件拷贝不足,应该把它们加入到cla ...

  10. io多路复用,select,笔记

    一下代码,是摘自大王的博客,(http://www.cnblogs.com/alex3714/)我自己有加了些注释. 1 2 3 #_*_coding:utf-8_*_ 4 5 __author__ ...