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 ...
随机推荐
- C#事务的使用
1.引入相应的命名空间 using System.Transactions; 2.代码事例(using (TransactionScope ts = new TransactionScope())) ...
- Response、Request、QueryString,repeater添加,修改,删除数据
内置对象: Response对象:响应请求,Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response.Write("<script>a ...
- 2600: [Ioi2011]ricehubh
Description 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺 ...
- “”?: H3C SSH 配置+
开启ssh 服务 ssh service enable 创建用户 使用ssh local-user ssh 用户级别 authorization-attribute user-role level-1 ...
- ubuntu 挂载新硬盘
http://www.cnblogs.com/hnrainll/archive/2012/02/27/2369331.html
- spring常见异常
1.ClassNotFoundException: org.springframework.dao.support.DaoSupport(解决:导入spring-tx) 2.NoClassDefFou ...
- javascript改变样式(cssFloat,styleFloat)
昨天遇到一用js改变元素浮动的,当时直接写了 obj.style.float="left";结果没起作用:查了资料后才发现不能这样写,现在整理下几种样式写法 1,直接写css属性的 ...
- 记一次SQLServer数据库误删数据找回
昨天 同事在本机清理数据库表时,连接到了生产机,误删了二十几张表,幸好是晚上加班的时候删除的,生产机上当时是一天一备份,还原备份是最后的策略,最关键的还是要找回数据. ...
- Heroku空项目 Git本地Push代码错误 以及 Heroku Web启动错误
在Eclipse下建了一个"Blank Heroku App", 然后将自己写好的JS Web练习代码直接复制放到了这个空项目下, 由于Eclipse下的git工具不太会用, 导致 ...
- Python爬虫 -- 抓取电影天堂8分以上电影
看了几天的python语法,还是应该写个东西练练手.刚好假期里面看电影,找不到很好的影片,于是有个想法,何不搞个爬虫把电影天堂里面8分以上的电影爬出来.做完花了两三个小时,撸了这么一个程序.反正蛮简单 ...