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
转载于:https://www.cnblogs.com/TenosDoIt/p/3774747.html
LeetCode:Spiral Matrix I II相关推荐
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 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
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [算法][LeetCode]Spiral Matrix
题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
- C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...
最新文章
- 【计算机网络】网络层 : ARP 协议 ( 使用 ARP 协议查找 目的主机 / 路由器 物理地址 )★
- 在使用net start mysql命令时提示键入 net helpmsg 3523错误
- docker - 在centos7和windows10安装
- wingIDE右侧文件列表移动到左侧
- java 随机生成图,Java中的快速实值随机生成器
- 华为愿出售5G技术渴望对手;苹果将向印度投资10亿美元;华为全联接大会首发计算战略;腾讯自研轻量级物联网操作系统正式开源……...
- 二、十六进制数互相转换
- [USACO13JAN] Seating
- MSRA,我的实习初体验(上)
- 强连通分量[trajan]
- 在IE情况下兼容 axios 的问题
- php如何隐藏入口文件,php怎么隐藏入口文件
- Android自定义选座,Android实现电影院选座效果
- 项目难题:Java几分钟处理完30亿个数据?
- 接口测试之postman
- opencv c++ 检测红色HSV 和RGB
- 微分几何学类毕业论文文献都有哪些?
- 华为云空间兑换码在哪里找_华为云空间在哪里找到
- 小白python爬虫入门实例2—— 翻页爬取京东商城商品数据
- 公众号裂变拉新,以婴儿辅食为诱饵,实现低成本获客!
热门文章
- 学会 IDEA REST Client后,postman就可以丢掉了...
- 面试官:你知道 Docker 有哪些优缺点嘛?
- 深入理解XGBoost,优缺点分析,原理推导及工程实现
- Datawhale x 科大讯飞 iFLYTEK A.I.开发者大赛重磅开启!
- 27岁清华留美博士疑似跳机自杀!:常年被抑郁症困扰
- 商汤被曝已获准在香港上市,计划筹资逾10亿美元
- 吴恩达老师机器学习和深度学习课程文字版下载
- 学习AI方向大半年,为什么你还没有别人几个月更精通?
- 80后博导当上双一流高校副校长:还是杰青获得者
- 规格选项表管理之保存规格选项表数据