LeetCode:Spiral Matrix I II
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的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 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 ...
- [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 ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- Leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- Linux下cutecom使用USB转串口线
http://www.cnblogs.com/pang123hui/archive/2011/05/29/2309888.html 在Linux下的串口调试一直使用minicom,虽说Linux的精髓 ...
- Android开源框架——Picasso
开篇——介绍Picasso (Picasso是什么?)Picasso:A Powerfull Image Downloading and Caching Library for Android,即An ...
- 在centos上编译安装mariadb数据库
一.安装前提(准备数据文件.安装其他依赖的软件) 1.准备数据存放的目录 [root@localhost ~]# fdisk /dev/sdb (fdisk /dev/sdb 创建一个逻辑分区/de ...
- Oracle数据库11g基于rehl6.5的配置与安装
REDHAT6.5安装oracle11.2.4 ORACLE11G R2官档网址: http://docs.oracle.com/cd/E11882_01/install.112/e24326/toc ...
- C++二级指针第三种内存模型
#include "stdio.h" #include "stdlib.h" #include "string.h" void main() ...
- MySql连接JDBC数据库------借鉴的红客联盟的
首先安装MySQL数据库,我安装的是MySQL5.5,具体安装步骤这里就不介绍了.需要提醒的是,如果安装进程一直停在start service那里,无法继续进行下去的话,请参照我的博文<安装My ...
- Cisco ASA intra-interface routing
LAN1和LAN2的默认路由指向各自的ASA,各ASA中设置对方LAN的静态路由指向ROUTER,打开ASA的intra-interface traffic,关闭LAN1和LAN2地址互相访问的NAT ...
- ARCGIS FOR JAVASCRIPT API 出现multipleDefine问题
问题: Error {src: "dojoLoader", info: Object, stack: (...), message: "multipleDefine&qu ...
- 尝试在Linux上编译KestrelHttpServer
Kestrel是目前在非Windows平台上运行ASP.NET 5应用程序的唯一可用Web服务器,但微软似乎将它冷落在一边,源代码更新很慢. 今天试着在Linux上编译Kestrel的源代码,遇到了很 ...
- MongoDB与.NET结合使用一(mongodb在windows 2003上的安装)
mongodb发展至今已经到2.6版本了,自从获得了1亿美元的风投之后,发展速度更是比以前快了很多,前段时间因为要用缓存,也比较了mongodb,大家也都觉得比较适合做无关系化的大数据存储,所以系统统 ...